GUIDs

Windows specific forum
stubbsi
User
User
Posts: 50
Joined: Tue Jul 04, 2006 8:59 pm
Location: Mt Martha, Australia

GUIDs

Post by stubbsi »

I feel like a bit of a dipstick here. I'm trying to use the cocreateguid/stringfromguid2 functionality, and I'm unable to do so, as a result of not fully understanding how to use DLL functionality. The normal, run of the mill stuff for me is working ok --- but this ..... :?

[I wish I had received my copy of Kale's book] :(

However, if someone could help, I'd really appreciate it.

Code: Select all

Enumeration
  #oledll
EndEnumeration



If OpenLibrary(#oledll, "ole32.dll")
  cocreateaddr.l = GetFunction(#oledll,"CoCreateGuid")
  stringguidaddr.l = GetFunction(#oledll,"StringFromGUID2")
  newguid.GUID
  result.l = CallFunctionFast(cocreateaddr,newguid)
  Dim a.b(100)
  leng.l = 40
  Redim a.b(leng*2-1)
  lr.l = CallFunctionFast(stringguidaddr,newguid,@a,leng)
   Debug lr
  stringguid.s
  For i.l = 0 To 39
   ; stringguid = stringguid + PeekS(@a(i))
    Debug PeekS(@a(i))
  Next
  ;Debug stringguid
Else
  Debug "ole32.dll not found"
EndIf
TIA
Vincit qui primum gerit
"The Old Farts Wins" or "He Conquers Who First Grows Old"
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Well, mr. O.F w. N.T, :lol:

you were pretty close. I massaged your code a bit, try this. Remember to check "unicode executable" in the compiler options:

Code: Select all

Enumeration 
  #oledll 
EndEnumeration 

If OpenLibrary(#oledll, "ole32.dll") 
  cocreateaddr.l = GetFunction(#oledll,"CoCreateGuid") 
  stringguidaddr.l = GetFunction(#oledll,"StringFromGUID2") 
  newguid.GUID 
  result.l = CallFunctionFast(cocreateaddr,newguid) 
  a.s=Space(78) 
  lr.l = CallFunctionFast(stringguidaddr,newguid,@a,78) 
  Debug a
Else 
  Debug "ole32.dll not found" 
EndIf 
Oh yeah, one other thing. You're using the registered version of PureBasic 4.0, right? If so, those functions are available directly from PB, you don't need to open the library. Just do CoCreateGuid_(... and it'll work. So the whole thing can be reduced to:

Code: Select all

a.s=Space(78) 
CoCreateGuid_(@newguid.GUID) 
StringFromGUID2_(newguid,@a,78) 
Debug a
BERESHEIT
stubbsi
User
User
Posts: 50
Joined: Tue Jul 04, 2006 8:59 pm
Location: Mt Martha, Australia

Post by stubbsi »

thanks - yep, I am registered.

Once again -- the forum answers a question.
Vincit qui primum gerit
"The Old Farts Wins" or "He Conquers Who First Grows Old"
stubbsi
User
User
Posts: 50
Joined: Tue Jul 04, 2006 8:59 pm
Location: Mt Martha, Australia

Post by stubbsi »

Maybe I'm nuts, but I only get a { as the GUID from the snippet above, and unicode is selected
Vincit qui primum gerit
"The Old Farts Wins" or "He Conquers Who First Grows Old"
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Both snips?

[edit] Are you absolutely certain unicode executable is checked? Because the output you describe is exactly what you get if it is not checked.
BERESHEIT
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

This run in Unicode and Ansi:

Code: Select all

Procedure.s MakeGUID()
  LBuffer.s = Space(76)
  CoCreateGuid_(GuidId.GUID)
  StringFromGUID2_(GuidId, LBuffer,76);
  CompilerIf #PB_Compiler_Unicode
    ProcedureReturn LBuffer
  CompilerElse
    SBuffer.s = Space(38)
    WideCharToMultiByte_(0, 0, LBuffer, 76, SBuffer, 38, 0, 0);
    ProcedureReturn SBuffer
  CompilerEndIf
EndProcedure

Debug MakeGUID()
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
stubbsi
User
User
Posts: 50
Joined: Tue Jul 04, 2006 8:59 pm
Location: Mt Martha, Australia

Post by stubbsi »

sorry - nonspecific - mind blasted after spending the last 4.5 hours hours driving my son to and from a consultant physio at a teaching hospital.

The second snippet
Vincit qui primum gerit
"The Old Farts Wins" or "He Conquers Who First Grows Old"
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Yes - good one ts-soft!
BERESHEIT
stubbsi
User
User
Posts: 50
Joined: Tue Jul 04, 2006 8:59 pm
Location: Mt Martha, Australia

Post by stubbsi »

Brilliant :D

Thanks ---
Vincit qui primum gerit
"The Old Farts Wins" or "He Conquers Who First Grows Old"
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

I was missing an @ symbol - but it was working here, which seems odd. Anyway, here's what it should be:

Code: Select all

a.s=Space(78) 
CoCreateGuid_(@newguid.GUID) 
StringFromGUID2_(@newguid,@a,78) 
Debug a
BERESHEIT
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

I think some api is defined as any-type. With any you can use string or pointer to string. Any is only in librarys avalaible

PB-Library SDK, readme.txt:
* Any: The parameter can be anything (the compiler won't check the type)
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

ts-soft wrote:This run in Unicode and Ansi:

Code: Select all

Procedure.s MakeGUID()
  LBuffer.s = Space(76)
  CoCreateGuid_(GuidId.GUID)
  StringFromGUID2_(GuidId, LBuffer,76);
  CompilerIf #PB_Compiler_Unicode
    ProcedureReturn LBuffer
  CompilerElse
    SBuffer.s = Space(38)
    WideCharToMultiByte_(0, 0, LBuffer, 76, SBuffer, 38, 0, 0);
    ProcedureReturn SBuffer
  CompilerEndIf
EndProcedure

Debug MakeGUID()
Is that a bad code :mrgreen:

Code: Select all

Procedure.s MakeGUID()
  Protected GUID.GUID, lpsz.s{76}
  If CoCreateGuid_(@GUID) = #S_OK
    StringFromGUID2_(@GUID, @lpsz, 76)
    ProcedureReturn  PeekS(@lpsz, 76, #PB_Unicode)
  EndIf
  ProcedureReturn ""
EndProcedure
New Version with Unicode- and Ansi-Support :D
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

or one line shorter:

Code: Select all

Procedure.s MakeGUID()
  Protected guid.GUID, lpsz.s{78}
  If CoCreateGuid_(@guid) = #S_OK
    ProcedureReturn PeekS(@lpsz, StringFromGUID2_(guid, @lpsz, 78), #PB_Unicode)
  EndIf
EndProcedure

Debug MakeGUID()
:)
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Flype wrote:or one line shorter:
No, two lines shorter, but if CoCreateGuid_ fails, the result is undefined, no
ProcedureReturn! :)
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

no, i guess no.

once i check for - If CoCreateGuid_(@guid) = #S_OK - it should be ok.

and i don't think that the StringFromGUID2() function can fails because all the needed ressources are already allocated when called.

and even if StringFromGUID2() fails, it returns 0 (zero bytes) so PeekS() can't fail with a 0 length.

:?: :idea:
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Post Reply