Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a DLL, whose functions i want to use in my c# code Here are the functions of that DLL :

C++
extern "C" 

{

 __declspec(dllimport) const char* __stdcall ZAJsonRequestA(const char *szReq);

__declspec(dllimport) const wchar_t* __stdcall ZAJsonRequestW(const wchar_t *szReq);

__declspec(dllimport) const BSTR __stdcall ZAJsonRequestBSTR(BSTR sReq); 

}



For the first function, i have used the syntax below :

My declaration is like this :
[DllImport("FANselect.dll", EntryPoint = "ZAJsonRequestA")]
private static extern string ZAJsonRequestA(string szReq);

and i try to invoke it using :
string sResult = ZAJsonRequestA(sRequest);

When i try to invoke the function, it goes out of the block and the following other statements donot get executed.

Can anyone tell me, what i am doing wrong here ?
how to use it in c# project, as this dll seems to be in other language ?
Posted
Updated 8-Apr-13 21:19pm
v3
Comments
Bernhard Hiller 9-Apr-13 3:05am    
Isn't that a COM dll? BSTR is typically used with COM. If so, register the dll (regsvr32.exe), and add a reference to it (in the COM tab of the referenes dialog).
amitt0488 9-Apr-13 3:16am    
I tried to register it, but it didn't registered.

Making a p/invoke import is not complicated in general. You can find more info and even find ready-made imports here: http://www.pinvoke.net/[^]
You can use some tools to help you build import signatures. Like these two:
http://clrinterop.codeplex.com/releases/view/17579[^]
http://www.red-gate.com/products/dotnet-development/pinvoke/[^]
 
Share this answer
 
So, finally i got the solution.

the declaration is like :

[DllImport("FANselect.dll", CallingConvention = CallingConvention.StdCall,
EntryPoint = "ZAJsonRequestA", ExactSpelling=false)]

private static extern IntPtr ZAJsonRequestA([MarshalAs(UnmanagedType.LPStr)]StringBuilder szReq);


To invoke it :

StringBuilder sbuilder = new StringBuilder();
sbuilder.Append(inputBox.Text);
string sResult = Marshal.PtrToStringAnsi(ZAJsonRequestA(sbuilder));

Thanks for your time guys!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900