在c#代码中导入DLL函数 [英] Importing DLL functions in c# code
问题描述
我有一个DLL,我希望在我的c#代码中使用它的功能以下是该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);
}
对于第一个函数,我使用了语法下面:
我的声明是这样的:
[DllImport(FANselect.dll,EntryPoint =ZAJsonRequestA)]
private static extern string ZAJsonRequestA(string szReq);
我尝试使用以下方法调用它:
string sResult = ZAJsonRequestA(sRequest);
当我尝试调用该函数时,它会离开块并且以下其他语句不会被执行。
任何人都可以告诉我,我在这里做错了什么?
如何在c#项目中使用它,因为这个dll似乎是用其他语言?
一般来说,进行ap / invoke导入并不复杂。您可以在此处找到更多信息,甚至可以找到现成的导入产品: http://www.pinvoke.net/ [ ^ ]
您可以使用一些工具来帮助您构建导入签名。像这两个:
http://clrinterop.codeplex.com/releases/view/17579 [ ^ ]
http://www.red-gate.com/products/dotnet-development/pinvoke/ [ ^ ]
所以,最后我得到了解决方案。
声明如下:
[DllImport(FANselect.dll,CallingConvention = CallingConvention.StdCall,
EntryPoint =ZAJsonRequestA,ExactSpelling = false)]
private static extern IntPtr ZAJsonRequestA([MarshalAs(UnmanagedType.LPStr)] StringBuilder szReq);
要调用它:
StringBuilder sbuilder = new StringBuilder();
sbuilder.Append(inputBox.Text);
string sResult = Marshal.PtrToStringAnsi(ZAJsonRequestA( sbuilder));
感谢您的时间!
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 ?
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/[^]
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!
这篇关于在c#代码中导入DLL函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!