Problem with wrapping a library

Just starting out? Need help? Post your questions and find answers here.
Gushasad
User
User
Posts: 20
Joined: Thu Aug 30, 2018 7:41 am

Problem with wrapping a library

Post 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)
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Problem with wrapping a library

Post 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.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Problem with wrapping a library

Post 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.
Gushasad
User
User
Posts: 20
Joined: Thu Aug 30, 2018 7:41 am

Re: Problem with wrapping a library

Post by Gushasad »

Wow! Now my client application (written in C #) throws an access violation exception. I really don't wanna use C ++ for this!
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Problem with wrapping a library

Post 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?
Gushasad
User
User
Posts: 20
Joined: Thu Aug 30, 2018 7:41 am

Re: Problem with wrapping a library

Post 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?
Gushasad
User
User
Posts: 20
Joined: Thu Aug 30, 2018 7:41 am

Re: Problem with wrapping a library

Post 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
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Problem with wrapping a library

Post by infratec »

I sent you a PM.
For somethink like this I prefer PMs.
Gushasad
User
User
Posts: 20
Joined: Thu Aug 30, 2018 7:41 am

Re: Problem with wrapping a library

Post by Gushasad »

Yo! I have replied. The DLL in question (ZZDCloudAPI) is written in C ++.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Problem with wrapping a library

Post by infratec »

For a C++ DLL you may need interfaces:

viewtopic.php?p=440419#p440419
viewtopic.php?p=412798#p412798
Gushasad
User
User
Posts: 20
Joined: Thu Aug 30, 2018 7:41 am

Re: Problem with wrapping a library

Post 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
Post Reply