Page 1 of 1

Problem with wrapping a library

Posted: Thu Sep 10, 2020 8:06 pm
by Gushasad
Yo! I am back to PB after some time of absence. I am wrapping a Windows DLL that is written in C ++ to be used with PB. I will show you the fragment of the documentation and then my attempt to wrap it. The DLL compiles, even the log is created if I call the DLL from another application, but the other functions are not executed.
Here is the fragment of the library's documentation
int Initial(wchar_t* appkey, wchar_t* seckey, ZDCloudAPICallBack zdcCallBack)
typedef void(WINAPI *ZDCloudAPICallBack)(int type);
[out] int type
int SpeakAsync(wchar_t* text, BOOL bWindowChangedStop)
And now my prototype definitions:
int SpeakAsync(wchar_t* text, BOOL bWindowChangedStop)
Below I am showing the procedure that is called on attaching my DLL to the current process:

Code: Select all

ProcedureDLL AttachProcess(Instance)
  OpenLibrary(0, "ZDCloudAPI.dll")
  Initial = GetFunction(0, "Initial")
  UnInitial = GetFunction(0, "UnInitial")
  SpeakAsync = GetFunction(0, "SpeakAsync")
  StopSpeak = GetFunction(0, "StopSpeak")
  CreateFile(1, "log.txt")
  result = Initial("apikey","secretkey",#Null)
  WriteInteger(1, result)
EndProcedure
It seems like the "Initial" function is not even called properly.
I am sorry for this chaotic post, but I really look desperately for the solution. I have tried using a "p-unicode" parameter instead of wchar_t, but that didn't work either.
The use of normal strings is the result of my todays battle with the problem.

// Edit: Code-Tags added (Kiffi)

Re: Problem with wrapping a library

Posted: Fri Sep 11, 2020 8:47 am
by infratec

Code: Select all


PrototypeC ZDCloudAPICallBack(type.i)

PrototypeC.i Proto_Initial(appkey.p-Unicode, seckey.p-Unicode, zdcCallBack.ZDCloudAPICallBack)
PrototypeC Proto_UnInitial()
PrototypeC.i Proto_SpeakAsync(text.p-Unicode, bWindowChangedStop.i)
PrototypeC Proto_StopSpeak()

Global ZDCloudAPI.i

Global Initial.Proto_Initial
Global UnInitial.Proto_UnInitial
Global SpeakAsync.Proto_SpeakAsync
Global StopSpeak.Proto_StopSpeak


Procedure Logging(Text$)
  Protected File.i
  
  File = OpenFile(#PB_Any, "log.txt", #PB_File_Append)
  If File
    WriteStringN(File, FormatDate("%yyyy%mm%dd %hh%ii%ss ", Date()) + Text$)
    CloseFile(File)
  EndIf
EndProcedure


Procedure AttachProcess()
  ZDCloudAPI = OpenLibrary(#PB_Any, "ZDCloudAPI.dll")
  If ZDCloudAPI
    Initial = GetFunction(ZDCloudAPI, "Initial")
    UnInitial = GetFunction(ZDCloudAPI, "UnInitial")
    SpeakAsync = GetFunction(ZDCloudAPI, "SpeakAsync")
    StopSpeak = GetFunction(ZDCloudAPI, "StopSpeak")
    
    result = Initial("apikey", "secretkey", #Null)
    Logging(Str(result))
  Else
    Logging("Was not able to load ZDCloudAPI.dll")
  EndIf
EndProcedure
Maybe you have to remove the C from the Prototypes. This depends on the dll.

Test it first as a normal programm before you try to put it in a dll.

Re: Problem with wrapping a library

Posted: Fri Sep 11, 2020 8:48 am
by Mijikai
I dont see why it should work (unless PB does more than tranfering the call).

MSDN:

Code: Select all

There are significant limits on what you can safely do in a DLL entry point. See General Best Practices for specific Windows APIs that are unsafe to call in DllMain. If you need anything but the simplest initialization then do that in an initialization function for the DLL. You can require applications to call the initialization function after DllMain has run and before they call any other functions in the DLL.

Re: Problem with wrapping a library

Posted: Fri Sep 11, 2020 2:08 pm
by Gushasad
Wow! Now my client application (written in C #) throws an access violation exception. I really don't wanna use C ++ for this!

Re: Problem with wrapping a library

Posted: Fri Sep 11, 2020 5:50 pm
by infratec
Sorry, but your answer tells me nothing.

Have you tested it as normal program not as dll?
Have you removed the C from the Prototype command?

Re: Problem with wrapping a library

Posted: Fri Sep 11, 2020 6:13 pm
by Gushasad
Hi,
I actually haven't used PrototypeC as I know the DLL uses STDCALL convention. I havent tried using it as a normal program yet. I will do it soon.
infratec wrote:Sorry, but your answer tells me nothing.

Have you tested it as normal program not as dll?
Have you removed the C from the Prototype command?

Re: Problem with wrapping a library

Posted: Sat Sep 19, 2020 6:13 pm
by Gushasad
Hi,
I am really sorry to ask this question, but I guess I will need more help.
Can someone contact me via E-Mail (nuno69a at gmail dot com) and try to help me diagnose my problem? The reason I am requesting a mail help is that this DLL requires some API information which I cannot publish

Re: Problem with wrapping a library

Posted: Sat Sep 19, 2020 8:21 pm
by infratec
I sent you a PM.
For somethink like this I prefer PMs.

Re: Problem with wrapping a library

Posted: Mon Sep 21, 2020 5:53 pm
by Gushasad
Yo! I have replied. The DLL in question (ZZDCloudAPI) is written in C ++.

Re: Problem with wrapping a library

Posted: Mon Sep 21, 2020 8:35 pm
by infratec
For a C++ DLL you may need interfaces:

viewtopic.php?p=440419#p440419
viewtopic.php?p=412798#p412798

Re: Problem with wrapping a library

Posted: Wed Sep 23, 2020 8:43 pm
by Gushasad
WHy would I need interfaces? This library is not OOP. It uses standard Win32 types and strucytures. The only exception is the callback.
infratec wrote:For a C++ DLL you may need interfaces:

viewtopic.php?p=440419#p440419
viewtopic.php?p=412798#p412798