[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Unicode)] public struct SDCConfig { [MarshalAs( UnmanagedType.ByValTStr,SizeConst = CONFIG_NAME_SZ)] public string configName; [MarshalAs(UnmanagedType.ByValTStr,SizeConst = SSID_SZ)] public字符串SSID; [MarshalAs(UnmanagedType.ByValTStr,SizeConst = CLIENT_NAME_SZ)] public string clientName; public Int32 txPower; public AUTH authType; public EAPTYPE eapType; public POWERSAVE powerSave; pu blic WEPTYPE wepType; public BITRATE bitRate; public RADIOMODE radioMode; public CRYPT userName; public CRYPT userPwd; 公共CRYPT PSK; 公共CRYPT WEPKeys; } [DllImport( " VPSummit.dll",CharSet = CharSet.Unicode)] public static extern SDCERR GetConfig([MarshalAs(Unmanaged.Type.LPArray)] byte [] name ,参考SDCCONFIG cfg)]; 和函数调用是: public bool ConfigTest { string configName =" VANPUTER"; byte [] Name = System.Text.ASCIIEncoding.ASCII.GetBytes(ConfigNam e + ''\ 0 ''); SDCConfig cfg = new SDCConfig(); SDCERR result = GetConfig(Name,cfg); return(result = SDCERR_SUCCESS); } 实际上这个特殊的函数没有给我一个NotSupportedException, 并且结果包含SDCERR_SUCCES S,但cfg不包含它应该的 配置值,所以我显然做了一些错误的事情 。其他类型SDCCERR,AUTH,EAPTYPE等定义为 中的枚举,C和C#以及CONFIG_NAME_SZ等都是常量值。我很满意这里的深度,所以任何建议都会非常受欢迎!谢谢。 Andy BakerThey are as follows:C .h header file - Structurestypedef struct _CRYPT {DWORD size;char buffer [120];DWORD offset;} CRYPT;typedef struct _SDCConfig {char configName[CONFIG_NAME_SZ];char SSID[SSID_SZ];char clientName[CLIENT_NAME_SZ];int txPower;AUTH authType;EAPTYPE eapType;POWERSAVE powerSave;WEPTYPE wepType;BITRATE bitRate;RADIOMODE radioMode;CRYPT userName;CRYPT userPwd;CRYPT PSK;CRYPT WEPKeys;} SDCConfigAnd the declaration for one of the functions isSDCERR GetConfig(char *name, SDCCONFIG *cfg);My C# code - Structures[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]public struct CRYPT{public uint size;[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 120)]public string buffer;public uint offset;}[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]public struct SDCConfig{[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CONFIG_NAME_SZ)]public string configName;[MarshalAs(UnmanagedType.ByValTStr, SizeConst = SSID_SZ)]public string SSID;[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CLIENT_NAME_SZ)]public string clientName;public Int32 txPower;public AUTH authType;public EAPTYPE eapType;public POWERSAVE powerSave;public WEPTYPE wepType;public BITRATE bitRate;public RADIOMODE radioMode;public CRYPT userName;public CRYPT userPwd;public CRYPT PSK;public CRYPT WEPKeys;}[DllImport("VPSummit.dll", CharSet = CharSet.Unicode)]public static extern SDCERR GetConfig([MarshalAs(Unmanaged.Type.LPArray)]byte [] name, ref SDCCONFIG cfg)];and the function call is:public bool ConfigTest{string configName = "VANPUTER";byte [] Name = System.Text.ASCIIEncoding.ASCII.GetBytes(ConfigNam e +''\0'');SDCConfig cfg = new SDCConfig();SDCERR result = GetConfig(Name, cfg);return (result = SDCERR_SUCCESS);}Actually this particular function does not give me a NotSupportedException,and result contains SDCERR_SUCCESS, but cfg does not contain theconfiguration values that it should, so I am obviously doing somethingwrong. The other types SDCCERR, AUTH, EAPTYPE etc are defined as enums inboth C and C# and CONFIG_NAME_SZ etc are constant values. I am well out ofmy depth here, so any advice will be very welcome! Thanks.Andy Baker 不,它可能是我猜的任何东西!No, it could be anything I guess! 它们如下: C .h头文件 - 结构 typedef struct _CRYPT { * * DWORD * * size; * * char * * * * * * buffer [120]; * * DWORD * * offset; } CRYPT; typedef struct _SDCConfig { * * char * * * * * * * * * * configName [CONFIG_NAME_SZ]; * * char * * * * * * * * * * SSID [SSID_SZ]; * * char * * * * * * * * * * clientName [CLIENT_NAME_SZ]; * * int * * * * * * * * * * * * txPower; * * AUTH * * * * * * * * authType; * * EAPTYPE * * * * * eapType; * * POWERSAVE * * powerSave; * * WEPTYPE * * * * * wepType; * * BITRATE * * * * * * bitRate; * * RADIOMODE * * radioMode; * * CRYPT * * * * * * * userName; * * CRYPT * * * * * * * userPwd; * * CRYPT * * * * * * * PSK; * * CRYPT * * * * * * * WEPKeys; } SDCC onfig 其中一个函数的声明是 SDCERR GetConfig(char * name,SDCCONFIG * cfg); 我的C#代码 - 结构 [StructLayout(LayoutKind.Sequential,CharSet = CharSet.Unicode)] public struct CRYPT { * * public uint size; * * [MarshalAs(UnmanagedType.ByValTStr,SizeConst = 120)] * *公共字符串缓冲区; * * public uint offset; } [StructLayout(LayoutKind.Sequential) ,CharSet = CharSet.Unicode)] public struct SDCConfig { * * [MarshalAs(UnmanagedType.ByValTStr,SizeConst = CONFIG_NAME_SZ)] * * public string configName; * * [MarshalAs(UnmanagedType.ByValTStr,SizeConst = SSID_SZ)] * * public string SSID; * * [MarshalAs(UnmanagedType.ByValTStr,SizeConst = CLIENT_NAME_SZ)] * * public string clientName; * * public Int32 txPo wer; * * public AUTH authType; * * public EAPTYPE eapType; * * public POWERSAVE powerSave; * * public WEPTYPE wepType; * * public BITRATE bitRate; * * public RADIOMODE radioMode; * * public CRYPT userName; * * public CRYPT userPwd; * * public CRYPT PSK; * * public CRYPT WEPKeys; } [DllImport(" VPSummit.dll",CharSet = CharSet.Unicode)] public static extern SDCERR GetConfig([MarshalAs] (Unmanaged.Type.LPArray)] byte [] name,ref SDCCONFIG cfg)]; 和函数调用是: public bool ConfigTest { * * string configName =" VANPUTER"; * * byte [] Name = System。 Text.ASCIIEncoding.ASCII.GetBytes(ConfigNam e + ''\'''); * * SDCConfig cfg = new SDCConfig(); * * SDCERR结果= GetConfig(名称,cfg); * *返回(结果= SDCERR_SUCCESS); }They are as follows:C .h header file - Structurestypedef struct _CRYPT {* * DWORD * *size;* * char * * * * * *buffer [120];* * DWORD * *offset;} CRYPT;typedef struct _SDCConfig {* * char * * * * * * * * * * configName[CONFIG_NAME_SZ];* * char * * * * * * * * * * SSID[SSID_SZ];* * char * * * * * * * * * * clientName[CLIENT_NAME_SZ];* * int * * * * * * * * * * * *txPower;* * AUTH * * * * * * * * authType;* * EAPTYPE * * * * * eapType;* * POWERSAVE * *powerSave;* * WEPTYPE * * * * *wepType;* * BITRATE * * * * * *bitRate;* * RADIOMODE * *radioMode;* * CRYPT * * * * * * * userName;* * CRYPT * * * * * * * userPwd;* * CRYPT * * * * * * * PSK;* * CRYPT * * * * * * * WEPKeys;} SDCConfigAnd the declaration for one of the functions isSDCERR GetConfig(char *name, SDCCONFIG *cfg);My C# code - Structures[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]public struct CRYPT{* * public uint size;* * [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 120)]* * public string buffer;* * public uint offset;}[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]public struct SDCConfig{* * [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CONFIG_NAME_SZ)]* * public string configName;* * [MarshalAs(UnmanagedType.ByValTStr, SizeConst = SSID_SZ)]* * public string SSID;* * [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CLIENT_NAME_SZ)]* * public string clientName;* * public Int32 txPower;* * public AUTH authType;* * public EAPTYPE eapType;* * public POWERSAVE powerSave;* * public WEPTYPE wepType;* * public BITRATE bitRate;* * public RADIOMODE radioMode;* * public CRYPT userName;* * public CRYPT userPwd;* * public CRYPT PSK;* * public CRYPT WEPKeys;}[DllImport("VPSummit.dll", CharSet = CharSet.Unicode)]public static extern SDCERR GetConfig([MarshalAs(Unmanaged.Type.LPArray)]byte [] name, ref SDCCONFIG cfg)];and the function call is:public bool ConfigTest{* * string configName = "VANPUTER";* * byte [] Name = System.Text.ASCIIEncoding.ASCII.GetBytes(ConfigNam e +''\0'');* * SDCConfig cfg = new SDCConfig();* * SDCERR result = GetConfig(Name, cfg);* * return (result = SDCERR_SUCCESS);} 这里立刻看错了一件事就是你所有的C结构 使用char字符串(即非Unicode),但你使用CharSet.Unicode 结合UnmanagedType.ByValTStr - 这将使它们全部 用于编组的Unicode,并且扭曲了以下字段的布局 (从现在开始它们突然是它们应该的两倍)。尝试使用CharSet.Ansi取代,看看是否有帮助。One thing that immediately looks wrong here is that all your C structsuse char strings (that is, non-Unicode), but you use CharSet.Unicodecombined with UnmanagedType.ByValTStr - that would make them allUnicode for marshalling, and skew the layout of following fields(since now they suddenly are twice as far as they ought to be). Tryusing CharSet.Ansi instead and see if that helps. 这篇关于将结构传递给非托管代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-03 07:25