Click here to Skip to main content
11,926,961 members (54,549 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++ C C# C#4.0 DLL
I have a DLL, whose functions i want to use in my c# code Here are the functions of that DLL :

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 8-Apr-13 21:50pm
Edited 8-Apr-13 22:19pm
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Making a p/invoke import is not complicated in general. You can find more info and even find ready-made imports here:[^]
You can use some tools to help you build import signatures. Like these two:[^][^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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();
string sResult = Marshal.PtrToStringAnsi(ZAJsonRequestA(sbuilder));

Thanks for your time guys!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.1 | Last Updated 9 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100