我需要用c#调用一个dll文件,这个dll文件是用VC写的,具体的功能就是传入一个字符串后,输出一个字符串,而且传入的和返回的定义类型都是char*,c#里我调用dll的代码是这样写的:
public class TransferDll
{
[DllImpor("GetString.DLL",EntryPoint="GetCString",CallingConvention=CallingConvention.Winapi)]
public static extern StringBuilder  GetCString(StringBuilder InuptString);
}
 调用dll里面的函数是这样写的:
.........
.........
string strTest = null;
StringBuilder test = new StringBuilder();
    
StringBuilder testwo = new StringBuilder();

testwo.Append("磁盘驱动器");
testwo.Append('/');
test= TransferDll.GetCString(testwo);
strTest = test.ToString();
........
........
但是我编译的时候,老是报未将对象引用设置到对象的实例的错误。
我昨天看了半天,也没看出来是怎么回事,
各位高手看看,到底是怎么回事,

解决方案 »

  1.   

    如果只是传进的话,你可以如下试试
    public class TransferDll
    {
    [DllImpor("GetString.DLL",EntryPoint="GetCString")]
    public static extern string GetCString( string InuptString);
    }
      

  2.   

    如果还有问题,你最好说说传入参数在dll中是否进行修改之类操作。
      

  3.   

    Knight94(愚翁)兄弟,谢谢了啊,这么及时,呵呵。
      我觉得老报这个错误,是不是我调用dll中的函数的时候,调用有错误?
      我试试你说的。
      我传入的参数在dll中要做转换,其实也没什么,dll中的函数起到一个翻译的功能,就是说,返回的参数是传入参数的其他国语言。
      

  4.   

    你试试如下这个是否报错
    public class TransferDll
    {
    [DllImpor("GetString.DLL",EntryPoint="GetCString")]
    public static extern IntPtr GetCString( string InuptString);
    }
      

  5.   

    to Knight94(愚翁)
     还是报一样的错,
      

  6.   

    用vc写的dll是不是需要注册一下才能用在c#中 我以前用vb写的一个dll用在c#中就注册了一下
    具体怎样我也忘记了,搜索一下试试
      

  7.   

    to binny0532(苏打水)
    我试试。
      

  8.   

    把传入参数的类型也修改为IntPtr试一下,还不行的话,设置一下CharSet,或者再看看你的vc代码
    public class TransferDll
    {
    [DllImpor("GetString.DLL",EntryPoint="GetCString")]
    public static extern IntPtr GetCString( IntPtr InuptString);
    }
      

  9.   

    to zlc_168(zlc)
    我试试。
      

  10.   

    StringBuilder test = new StringBuilder(1024);
        
    StringBuilder testwo = new StringBuilder(1024);指定一个大小试试
      

  11.   

    不过,比如我要输入的是磁盘驱动器,那么怎么转换为intptr呢?
    呵呵,
      

  12.   

    现在可以调用了,但是又出现了一个问题:
    调用函数为:
    public static extern IntPtr GetCString( IntPtr InuptString);
    返回的是一些数字,而实际上应该返回输入参数的英文。
    而调用函数为:
    public static extern string GetCString( IntPtr InuptString);
    返回的是乱码,应该是类型不匹配,
    大家再看看,有没有好的解决办法。
      

  13.   

    试试这2个
    DllImpor("GetString.DLL",EntryPoint="GetCString")]
    public static extern string GetCString( ref string InuptString);
     或
    DllImpor("GetString.DLL",EntryPoint="GetCString")]
    public static extern string GetCString(ref StringBuilder InuptString);
    用IntPtr 是错误的
      

  14.   

    笔误,不好意思,第二个应该是:
    [DllImpor("GetString.DLL",EntryPoint="GetCString")]
    public static extern StringBuilder GetCString(ref StringBuilder InuptString);
      

  15.   

    char*在C#里对应的类型为string或StringBuilder,
    若参数传入后经修改后返回就需要添加ref
      

  16.   

    http://hi.baidu.com/boshiclub
    我们一起创业,我们一起发财,我们一起快乐,我们一起努力!
      

  17.   

    要我说,为啥不拿C#重写了那个DLL。