Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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
v3
Comments
Bernhard Hiller at 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 at 9-Apr-13 3:16am
   
I tried to register it, but it didn't registered.
Rate this: bad
good
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: 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/[^]
  Permalink  
Rate this: bad
good
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();
sbuilder.Append(inputBox.Text);
string sResult = Marshal.PtrToStringAnsi(ZAJsonRequestA(sbuilder));
 
Thanks for your time guys!
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 584
1 Maciej Los 275
2 DamithSL 233
3 Sergey Alexandrovich Kryukov 209
4 BillWoodruff 200
0 OriginalGriff 6,803
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,421
3 Manas Bhardwaj 4,841
4 Maciej Los 4,330


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 9 Apr 2013
Copyright © CodeProject, 1999-2014
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