MFCdemo中
结构体定义
typedef struct _TagDEVINPUTCONFIG
{   
BOOL  X0;      
BOOL  Y0;     
BOOL  XLN;  
BOOL  XLP;   
BOOL  YLN;  
BOOL  YLP;  
BOOL  START; 
BOOL  PAUSE; 
} DEVINPUTCONFIG, *PDEVINPUTCONFIG;函数调用引用
extern "C" AFX_EXT_API VOID gf_Init(LPCTSTR strWorkPath, PDEVINPUTCONFIG pInputConfig = NULL);
我现在C#是这么做的:
 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        public struct PDEVCONFIG
        {
            [MarshalAs(UnmanagedType.Bool)]
            bool X0;   
            [MarshalAs(UnmanagedType.Bool)]
            bool Y0;   
            [MarshalAs(UnmanagedType.Bool)]
            bool XLN; 
            [MarshalAs(UnmanagedType.Bool)]
            bool XLP; 
            [MarshalAs(UnmanagedType.Bool)]
            bool YLN;
            [MarshalAs(UnmanagedType.Bool)]
            bool YLP; 
            [MarshalAs(UnmanagedType.Bool)]
            bool START;
            [MarshalAs(UnmanagedType.Bool)]
            bool PAUSE;
        }       
      
        [DllImport("EIO", EntryPoint = "gf_Init", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
        public static extern void gf_Init(string PathName, ref PDEVCONFIG pInputConfig);函数可以正常跑过去,但是实际没有调用成功。现在不知道改怎么解决这个问题一点方向都没有。

解决方案 »

  1.   

    原函数是不是接收宽字符的? 如果不是就用 CharSet.Ansi
      

  2.   

    c++ BOOL 不等于 C# bool 
      

  3.   

    c++ BOOL 转为C# 的用Int32
      

  4.   


            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            public struct PDEVCONFIG
            {            [MarshalAs(UnmanagedType.I4)]
                Int32 X0;   
                [MarshalAs(UnmanagedType.I4)]
                Int32 Y0;    
                [MarshalAs(UnmanagedType.I4)]
                Int32 XLN ; // X 轴负限位是否存在
                [MarshalAs(UnmanagedType.I4)]
                Int32 XLP ;
                [MarshalAs(UnmanagedType.I4)]
                Int32 YLN ;
                [MarshalAs(UnmanagedType.I4)]
                Int32 YLP ;
                [MarshalAs(UnmanagedType.I4)]
                Int32 START ;
                [MarshalAs(UnmanagedType.I4)]
                Int32 PAUSE ;
            }       这么改了后,依旧不行。载入的参数还是默认参数。
      

  5.   

    如果原函数指明是使用Unicode字符集,而不是多字节字符集,那才要 CharSet = CharSet.Unicode,否则一定得是 CharSet = CharSet.Ansi。
    bool 配合 UnmanagedType.Bool 没问题,UnmanagedType.Bool 看解释,会翻译成4字节,但是程序目标平台需要指定 x86 ,不能是 AnyCPU
    另外你的描述“实际没有调用成功”,具体什么现象?
      

  6.   

    加入Pack=1试试[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode,Pack = 1)]
    public struct PDEVCONFIG
    {
    ...
    }
      

  7.   

      [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode,Pack=1)]
            public struct PDEVCONFIG
            {            [MarshalAs(UnmanagedType.I4)]
                Int32 X0;   
                [MarshalAs(UnmanagedType.I4)]
                Int32 Y0;    
                [MarshalAs(UnmanagedType.I4)]
                Int32 XLN ; 
                [MarshalAs(UnmanagedType.I4)]
                Int32 XLP ; 
                [MarshalAs(UnmanagedType.I4)]
                Int32 YLN ; 
                [MarshalAs(UnmanagedType.I4)]
                Int32 YLP ; 
                [MarshalAs(UnmanagedType.I4)]
                Int32 START ; 
                [MarshalAs(UnmanagedType.I4)]
                Int32 PAUSE ;
            }       
      

  8.   

    路径也确认了没有问题,同一路径下MFC调用出来的参数是正确的。
      

  9.   

    没有报错。但是调用出来的系统信息是默认信息,而相同路径MFC的DEMO程序调用出来的是我事先配置的信息。就是说根本没有找到配置文档。