PureBasic Forum
http://forums.purebasic.com/english/

Problem with wrapping a library
http://forums.purebasic.com/english/viewtopic.php?f=13&t=75939
Page 1 of 1

Author:  Gushasad [ Thu Sep 10, 2020 8:06 pm ]
Post subject:  Problem with wrapping a library

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:
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)

Author:  infratec [ Fri Sep 11, 2020 8:47 am ]
Post subject:  Re: Problem with wrapping a library

Code:

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.

Author:  Mijikai [ Fri Sep 11, 2020 8:48 am ]
Post subject:  Re: Problem with wrapping a library

I dont see why it should work (unless PB does more than tranfering the call).

MSDN:
Code:
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.

Author:  Gushasad [ Fri Sep 11, 2020 2:08 pm ]
Post subject:  Re: Problem with wrapping a library

Wow! Now my client application (written in C #) throws an access violation exception. I really don't wanna use C ++ for this!

Author:  infratec [ Fri Sep 11, 2020 5:50 pm ]
Post subject:  Re: Problem with wrapping a library

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?

Author:  Gushasad [ Fri Sep 11, 2020 6:13 pm ]
Post subject:  Re: Problem with wrapping a library

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?

Author:  Gushasad [ Sat Sep 19, 2020 6:13 pm ]
Post subject:  Re: Problem with wrapping a library

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

Author:  infratec [ Sat Sep 19, 2020 8:21 pm ]
Post subject:  Re: Problem with wrapping a library

I sent you a PM.
For somethink like this I prefer PMs.

Author:  Gushasad [ Mon Sep 21, 2020 5:53 pm ]
Post subject:  Re: Problem with wrapping a library

Yo! I have replied. The DLL in question (ZZDCloudAPI) is written in C ++.

Author:  infratec [ Mon Sep 21, 2020 8:35 pm ]
Post subject:  Re: Problem with wrapping a library

For a C++ DLL you may need interfaces:

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

Author:  Gushasad [ Wed Sep 23, 2020 8:43 pm ]
Post subject:  Re: Problem with wrapping a library

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:

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/