Page 1 of 2

GUIDs

Posted: Thu Aug 17, 2006 4:08 am
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

Posted: Thu Aug 17, 2006 4:52 am
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

Posted: Thu Aug 17, 2006 8:19 am
by stubbsi
thanks - yep, I am registered.

Once again -- the forum answers a question.

Posted: Thu Aug 17, 2006 8:26 am
by stubbsi
Maybe I'm nuts, but I only get a { as the GUID from the snippet above, and unicode is selected

Posted: Thu Aug 17, 2006 8:33 am
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.

Posted: Thu Aug 17, 2006 8:37 am
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()

Posted: Thu Aug 17, 2006 8:38 am
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

Posted: Thu Aug 17, 2006 8:39 am
by netmaestro
Yes - good one ts-soft!

Posted: Thu Aug 17, 2006 8:41 am
by stubbsi
Brilliant :D

Thanks ---

Posted: Thu Aug 17, 2006 8:48 am
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

Posted: Thu Aug 17, 2006 8:53 am
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)

Posted: Fri Mar 30, 2007 4:19 am
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

Posted: Fri Mar 30, 2007 8:49 am
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()
:)

Posted: Fri Mar 30, 2007 10:12 am
by ts-soft
Flype wrote:or one line shorter:
No, two lines shorter, but if CoCreateGuid_ fails, the result is undefined, no
ProcedureReturn! :)

Posted: Fri Mar 30, 2007 10:59 am
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: