读书人

C#调用Dephi的DLL疑问

发布时间: 2012-05-28 17:59:33 作者: rapoo

C#调用Dephi的DLL疑问,在线等
function GetVcell_Index(Devicechannum,IndexSs:Integer;var mV:widestring):Integer;stdcall;
传递参数:Devicechannum: 通道号 (只有一个通道,这里可以传1)
IndexSs:第几串
mV:(回传值)
返回参数:=0 表明不成功
=1 表明成功

这是他的调用函数,那么我C#调用该如何调用,因为这个包含了2个返回值,就蒙了。。
C#大致应该如下吧?
[DllImport("UserDll.dll", CallingConvention = CallingConvention.StdCall)]
// public static extern int GetVcell_Index(string OrignStr, string Key, int NewStr);
以上仅供参考,能力有限,还请大虾帮忙!

[解决办法]
public static extern int GetVcell_Index(int Devicechannum,int IndexSs,ref string mV);
试试
[解决办法]
话说一种语言去调用一种语言是很苦难的事情
所以才有COM COM+ 到现在.NET

可以试试楼上的
[解决办法]
public static extern int GetVcell_Index(int Devicechannum,int IndexSs,ref string mV);

string mV = "";
int Val = 0;
Val = GetVcell_Index(1,1,ref mV);

Val是返回值
[解决办法]

探讨

因为还没法接上设备仿真,只是进行了调用对方的DLL,出现了以下错误
无法加载 DLL“UserDll.dll”: 动态链接库(DLL)初始化例程失败。 (异常来自 HRESULT:0x8007045A)。
这个错误是来自于我程序本身,还是没有跟对方进行设备连接才会产生的。

[解决办法]
楼主可以使用loadlibrary去加载该动态链接库
如果加载都失败,那就容易判断问题了
[解决办法]
C# code
public delegate int FARPROC();[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]public struct HINSTANCE__ {        /// int    public int unused;}public partial class NativeMethods {        /// Return Type: HMODULE->HINSTANCE->HINSTANCE__*    ///lpFileName: LPCTSTR->LPCWSTR->WCHAR*    [System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint="LoadLibraryW")]public static extern  System.IntPtr LoadLibraryW([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPTStr)] string lpFileName) ;        /// Return Type: FARPROC    ///hModule: HMODULE->HINSTANCE->HINSTANCE__*    ///lpProcName: LPCSTR->CHAR*    [System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint="GetProcAddress")]public static extern  FARPROC GetProcAddress([System.Runtime.InteropServices.InAttribute()] System.IntPtr hModule, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string lpProcName) ;} 

读书人网 >C#

热点推荐