解决方案 »

  1.   

    把CharSet = CharSet.Unicode改成CharSet = CharSet.Ansi
      

  2.   

    改过了,老兄。CharSet = CharSet.Auto也试过。
      

  3.   

    另外,参数Int16 nport改成Int32 nport或int nport
      

  4.   


    最早是int32的,后来没办法了才换成Int16,不是这个问题。
      

  5.   


    最早是int32,后来没办法了才改成int16,问题不出在这里,感觉第一个参数的问题。
      

  6.   

    Int16 nport改成Int32试试
    大多数C++编译器int还是4字节的,64位机器上开发的,还有可能是8字节
      

  7.   

    那就用dumpbin /exports看看C++ dll的name是什么,是不是跟EntryPoint = "SetServerAddressAndPort"里的name一样,不一样就改成exports之后的name
      

  8.   


    我这个是64位的操作系统,win7的,换回了int,还是那个问题。我到cdeproject上搜了搜,也没有什么好的办法,郁闷。
      

  9.   

    你StringBuilder是怎么写的???这个C里面的Char*对应StringBuilder应该不会有错的...
      

  10.   


    我用depends.exe 查看了dll,,这个是没有错误的。
      

  11.   


            [DllImport("ImApi.dll", EntryPoint = "SetServerAddressAndPort", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public static extern void SetServerAddressAndPort(StringBuilder sip, int nport);  int port = 5123;
                StringBuilder sb = new StringBuilder(256);
                sb.Append("124.205.114.11");
                SetServerAddressAndPort(sb, port);
    这样写没错吧。
      

  12.   


            [DllImport("ImApi.dll", EntryPoint = "SetServerAddressAndPort", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public static extern void SetServerAddressAndPort(StringBuilder sip, int nport);  int port = 5123;
                StringBuilder sb = new StringBuilder(256);
                sb.Append("124.205.114.11");
                SetServerAddressAndPort(sb, port);
    这样写没错吧。还是不行的。
      

  13.   


            [DllImport("ImApi.dll", EntryPoint = "SetServerAddressAndPort", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public static extern void SetServerAddressAndPort(StringBuilder sip, int nport);  int port = 5123;
                StringBuilder sb = new StringBuilder(256);
                sb.Append("124.205.114.11");
                SetServerAddressAndPort(sb, port);
    这样写没错吧。
    这样还是报内存错误的话,你要查查const char *和char *的区别了...我在用c写的函数时碰见char *我都是用StringBuilder没有问题...
      

  14.   


            [DllImport("ImApi.dll", EntryPoint = "SetServerAddressAndPort", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public static extern void SetServerAddressAndPort(StringBuilder sip, int nport);  int port = 5123;
                StringBuilder sb = new StringBuilder(256);
                sb.Append("124.205.114.11");
                SetServerAddressAndPort(sb, port);
    这样写没错吧。
    这样还是报内存错误的话,你要查查const char *和char *的区别了...我在用c写的函数时碰见char *我都是用StringBuilder没有问题...
    char*  对应 string 是没错的。因为 char*  p ="sdfsdf" ,他指向的一个字符串的指针,这个是个const  char*
      

  15.   


    ref 是个引用,我刚试过,不行的。这是个入参,应该不是这个问题。
      

  16.   

    http://wenku.baidu.com/link?url=5bOg3P3muM1fVKUrwxMzZotbS35lxV81oj960z5ilox41zoFmmZLSl5MZctPTUHwFgsosXXQJ6XDyzi5HScypOwM_yR02CLR_VH-U3Ezlve看这个连接里面给的
      

  17.   

    文章中说的是string 或stringbuilder对应const  char*,我感觉也是没错的。就是过不去。
    先吃饭去,下午继续研究。
      

  18.   

    C++的int对应的是Int32,写Int16肯定不对。const char*或者char*一般用string是没错的。
    你试着在C++中调用下,看看别个SDK有没有问题。
    另外直接这样[DllImport("ImApi.dll")],一般用stdcall的不需要加别的属性。
      

  19.   


    都试过了,还是无解。c++中是这样写的,可以通过
            CString IPStr;
      int nIPRet        = GetDlgItemText(IDC_IP_EDIT,IPStr);
    std::string sIP = IPStr.GetBuffer(0);
    CString PortStr;
    int nPortRet      = GetDlgItemText(IDC_PORT_EDIT, PortStr);
    std::string sPort = PortStr.GetBuffer(0);
    SetServerAddressAndPort(sIP.c_str(), stringToInt(sPort));
      

  20.   

    都试过了,还是无解。c++中是这样写的,可以通过
            CString IPStr;
      int nIPRet        = GetDlgItemText(IDC_IP_EDIT,IPStr);
    std::string sIP = IPStr.GetBuffer(0);
    CString PortStr;
    int nPortRet      = GetDlgItemText(IDC_PORT_EDIT, PortStr);
    std::string sPort = PortStr.GetBuffer(0);
    SetServerAddressAndPort(sIP.c_str(), stringToInt(sPort));但是c#怎么就过不去呢?困扰啊!~
      

  21.   

    CallingConvention = CallingConvention.StdCall 改成CallingConvention = CallingConvention.Cdecl  试试?
      

  22.   

    第一个参数用 IntPtr 试试。   或加 ref intptr 或加out intptr  多试试吧
      

  23.   

    试试这个:
     public static extern void SetServerAddressAndPort([MarshalAs(UnmanagedType.LPStr)]string sip, Int16 nport);
    跟“数据封送”有关,msdn上有详细的例子,查查看
      

  24.   

    回复于: 2014-10-28 16:44:49
    引用 27 楼 siszqsqx2 的回复:
    试试这个:
     public static extern void SetServerAddressAndPort([MarshalAs(UnmanagedType.LPStr)]string sip, Int16 nport);
    跟“数据封送”有关,msdn上有详细的例子,查查看试过了。实在没招了。将sip 改成stringBuilder也不行?
      

  25.   

    都试过了,还是无解。c++中是这样写的,可以通过
            CString IPStr;
      int nIPRet        = GetDlgItemText(IDC_IP_EDIT,IPStr);
    std::string sIP = IPStr.GetBuffer(0);
    CString PortStr;
    int nPortRet      = GetDlgItemText(IDC_PORT_EDIT, PortStr);
    std::string sPort = PortStr.GetBuffer(0);
    SetServerAddressAndPort(sIP.c_str(), stringToInt(sPort));但是c#怎么就过不去呢?困扰啊!~那估计不是这里出的问题。有没有前置工作没有做,然后导致的内部出问题了。
      

  26.   

     [DllImport("ImApi.dll", EntryPoint = "SetServerAddressAndPort", CharSet = CharSet.Unicode]
      public static extern void SetServerAddressAndPort([In] byte[] sip, int nport);
             :
    byte[] spi=System.Text.Encoding.ASCII.GetBytes("124.205.114.11");
    int iPort=80;
    SetServerAddressAndPort(spi,iPort);
      

  27.   

    都试过了,还是无解。c++中是这样写的,可以通过
            CString IPStr;
      int nIPRet        = GetDlgItemText(IDC_IP_EDIT,IPStr);
    std::string sIP = IPStr.GetBuffer(0);
    CString PortStr;
    int nPortRet      = GetDlgItemText(IDC_PORT_EDIT, PortStr);
    std::string sPort = PortStr.GetBuffer(0);
    SetServerAddressAndPort(sIP.c_str(), stringToInt(sPort));但是c#怎么就过不去呢?困扰啊!~那估计不是这里出的问题。有没有前置工作没有做,然后导致的内部出问题了。
    第三方的dll,要是有源码就好了。
      

  28.   


    这样写还是报那个该死的 “尝试读取或写入受保护的内存。这通常指示其他内存已损坏”的错误。这个dll还有个函数叫GetServerAddressAndPort
    [DllImport("ImApi.dll",EntryPoint = "GetServerAddressAndPort", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
            public static extern void GetServerAddressAndPort(byte[] sip, int nLen,byte[] nport);
    这个调用已经解决了。
    byte[] sipbyte;
                byte[] sport;            sipbyte = new byte[256];
                sport = new byte[10];
                
                GetServerAddressAndPort(sipbyte, 256, sport);
                //C#调用dll时的类型转换总结
                string str = System.Text.Encoding.Default.GetString(sport);
                string sip = System.Text.Encoding.Default.GetString(sipbyte);
                int    port = bytesToInt(sport);
                MessageBox.Show("端口为"+port.ToString());
                MessageBox.Show("IP为" + sip);就剩下了这个 SetServerAddressAndPort
      

  29.   

    都试过了,还是无解。c++中是这样写的,可以通过
            CString IPStr;
      int nIPRet        = GetDlgItemText(IDC_IP_EDIT,IPStr);
    std::string sIP = IPStr.GetBuffer(0);
    CString PortStr;
    int nPortRet      = GetDlgItemText(IDC_PORT_EDIT, PortStr);
    std::string sPort = PortStr.GetBuffer(0);
    SetServerAddressAndPort(sIP.c_str(), stringToInt(sPort));但是c#怎么就过不去呢?困扰啊!~那估计不是这里出的问题。有没有前置工作没有做,然后导致的内部出问题了。问题解决了 。还是大仙说的对。不是这里出的问题。首先有个初始化的函数,执行完那个函数,这个就不报错了。最终的类型是string 的。
            [DllImport("ImApi.dll", EntryPoint = "SetServerAddressAndPort", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
            public static extern void SetServerAddressAndPort(string sip, int nport);总结:大仙之道,贵在修行呀!~
      

  30.   

    都试过了,还是无解。c++中是这样写的,可以通过
            CString IPStr;
      int nIPRet        = GetDlgItemText(IDC_IP_EDIT,IPStr);
    std::string sIP = IPStr.GetBuffer(0);
    CString PortStr;
    int nPortRet      = GetDlgItemText(IDC_PORT_EDIT, PortStr);
    std::string sPort = PortStr.GetBuffer(0);
    SetServerAddressAndPort(sIP.c_str(), stringToInt(sPort));但是c#怎么就过不去呢?困扰啊!~那估计不是这里出的问题。有没有前置工作没有做,然后导致的内部出问题了。问题解决了 。还是大仙说的对。不是这里出的问题。首先有个初始化的函数,执行完那个函数,这个就不报错了。最终的类型是string 的。
            [DllImport("ImApi.dll", EntryPoint = "SetServerAddressAndPort", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
            public static extern void SetServerAddressAndPort(string sip, int nport);总结:大仙之道,贵在修行呀!~弄好就好。