调用Dll时提示“未将对象引用设置到对象的实例”(50分)

H

hds6400

Unregistered / Unconfirmed
GUEST, unregistred user!
现有一个用Delphi写的dll为AutoWBPY.DLL,函数如下:
getbm( var str : shortstring ;
sel : integer);
在C#中采用以下两种方式调用都提示错误"未将对象引用设置到对象的实例":
方法一:
[DllImport("AutoWBPY.dll",EntryPoint="getbm",CharSet=CharSet.Auto,CallingConvention=CallingConvention.ThisCall)]
public unsafe static extern void getbm(ref string str,int sel);
private void button1_Click(object sender, System.EventArgs e)
{
string str=textBox2.Text;
unsafe
{
getbm(ref str,3);
}
}

方法二:
[DllImport("AutoWBPY.dll",EntryPoint="getbm",CharSet=CharSet.Auto,CallingConvention=CallingConvention.ThisCall)]
public unsafe static extern void getbm(char* str,int sel);
private void button1_Click(object sender, System.EventArgs e)
{
string str=textBox2.Text;
unsafe
{
fixed(char* temp=str)
{
getbm(temp,3);
}
}
}
 
C#應該不能直接調用delphi的dll,除非是delphi.net寫的dll.
 
应该可以动态联接库是通用的,你先注册一下再试试。
 
我有一次调用动态库时和你一样的错误
不过我是调用byte数组
关注!!!
 
这个错误事实上只是一个假像,就好像WINDOWS的一般性保护错一样,算是一个通用性的错误吧!
用C#是可以调用普通DLL文件的,可能是你在函数声明时参数类型不太对.你再检查一下.
 
to lykyl:请问该如何申明呢?
 
你在Delphi写的函数getbm( var str : shortstring ;
sel : integer);中shortstring是delphi中的类型所以在别的语言调用当然不行了,你在函数过程中可以使用VCL中的类,可是在导出时一定不能导出VCL中的东西.应该使用PChar.改成: procedure getbm(str: pchar;sel: integer);这样你再调用就不会有问题了.C#调用别的语言写的DLL是没有一点问题的,只要DLL写的标准.
 
多人接受答案了。
 
顶部 底部