api结构如下:
pDeliverResp是保存短消息的DeliverResp结构指针,DeliverResp定义如下:#pragma pack(push,_CTRL_BLOCK)
#pragma pack(1)
typedef struct
{
char SmsgID[11];
int NmsgFormat;
}DeliverResp;
#pragma pack(pop,_CTRL_BLOCK)
函数原型:DWORD SMGPDeliver(int nTimeout, DeliverResp * pDeliverResp)C#中我声明了第二个参数为ref,结构
struct strucDeliver
{
string SmsgID;int NmsgFormat;
}调用时返回:
struDeliver_resp deliver_resp=new struDeliver_resp();
i= SMGPDeliver( 5000,ref deliver_resp );
错误 :未能对象引用返回到对象实例。
麻烦帮解答,。

解决方案 »

  1.   

    要看你在C#里是如何重新声明接口的,假定你的dll名称为smgp.dll,路径为当前应用程序执行目录下,函数声明如下:
    [DllImport("smgp.dll",CharSet=CharSet.Auto)]
    static extern unsigned long SMGPDeliver(int nTimeout,ref DeliverResp delresp);
    对于API中的struct中的数组,你需要指定其长度,在C#里可以这样指定
    [ StructLayout( LayoutKind.Sequential )]
    public struct DeliverResp 
    {
       [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=11)] 
       public string SmsgID;
       public int NmsgFormat;
    }
      

  2.   

    p/invoke方面的知识,你可以查看msdn的platform invoke