首页 新闻 会员 周边 捐助

C#调用ocx错误

0
悬赏园豆:200 [已解决问题] 解决于 2014-05-23 10:09

我现在需要调用厂家提供的一个ocx控件,注册完之后,然后VS里添加com引用,其中有一个方法

1 [DispId(338)]        int AddOnePerson(object PersonInfo);//这个方法是VS自动生成的
View Code

其中PersonInfo结构厂商Demo中有定义:

 1   //Personnel Info
 2   typedef struct _PersonInfo
 3   {  
 4     long PersonnelID;
 5     long DeptID;
 6     long GroupID;
 7     TMyNameWideString LastName;
 8     TMyNameWideString FirstName;
 9     TMyNameWideString PersonnelCode;
10     TMyNameWideString Gander;
11     TMyNameWideString IDCard;
12     SYSTEMTIME Birthday;
13     TMyNameWideString Marriage;
14     wchar_t Nativeplace[30];
15     wchar_t Nation[30];
16     SYSTEMTIME AccessionDate;
17     SYSTEMTIME DimissionDate;
18     wchar_t Duty[30];
19     TMyNameWideString Diploma;
20     wchar_t School[30];
21     wchar_t Speciality[30];
22     TMyLongNameWideString Address;
23     TMyNameWideString Phone;
24     TMyNameWideString Email;
25     TMyLongNameWideString Remark;
26     long FloorNumber;
27     TMyLongNameWideString CustomField1;
28     TMyLongNameWideString CustomField2;
29     TMyLongNameWideString CustomField3;
30     TMyLongNameWideString CustomField4;
31     TMyLongNameWideString CustomField5;
32     TMyLongNameWideString CustomDict1;
33     TMyLongNameWideString CustomDict2;
34     TMyLongNameWideString CustomDict3;
35     TMyLongNameWideString CustomDict4;
36     TMyLongNameWideString CustomDict5;
37     boolean HasPhoto;
38     boolean IsLose;
39     boolean IsStop;
40     boolean Reserve;
41   } TPersonInfo, *PPersonInfo;
42 
43 TMyLongNameWideString 结构定义:
44   typedef struct _MyLongNameWideString
45   {
46       wchar_t Names[50];
47   } TMyLongNameWideString, *PMyLongNameWideString;
48 
49 TMyNameWideString 结构定义:
50   typedef struct _MyNameWideString
51   {
52       wchar_t Names[20];
53   } TMyNameWideString, *PMyNameWideString;
54 
55 
56 
57 Demo中调用是这样的:
58 long CSphinxOPCClientX::AddOnePerson(const VARIANT& PersonInfo)
59 {
60     long result;
61     static BYTE parms[] =
62         VTS_VARIANT;
63     InvokeHelper(0x152, DISPATCH_METHOD, VT_I4, (void*)&result, parms,
64         &PersonInfo);
65     return result;
66 }
View Code

我自己写的一个C#程序是这样使用的:

 1   [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
 2     public struct TPersonInfo
 3     {
 4         public int PersonnelID;
 5         public int DeptID;
 6         public TMyNameWideString LastName;
 7         public TMyNameWideString FirstName;
 8         public TMyNameWideString PersonnelCode;
 9         public TMyNameWideString Gander;
10         public TMyNameWideString IDCard;
11         public SYSTEMTIME Birthday;
12         public TMyNameWideString Marriage;
13         [MarshalAs(UnmanagedType.LPWStr, SizeConst = 30)]
14         public string Nativeplace;
15         [MarshalAs(UnmanagedType.LPWStr, SizeConst = 30)]
16         public string Nation;
17         public SYSTEMTIME AccessionDate;
18         public SYSTEMTIME DimissionDate;
19         [MarshalAs(UnmanagedType.LPWStr, SizeConst = 30)]
20         public string Duty;
21         public TMyNameWideString Diploma;
22         [MarshalAs(UnmanagedType.LPWStr, SizeConst = 30)]
23         public string School;
24         [MarshalAs(UnmanagedType.LPWStr, SizeConst = 30)]
25         public string Speciality;
26         public TMyLongNameWideString Address;
27         public TMyLongNameWideString Phone;
28         public TMyLongNameWideString Email;
29         public TMyLongNameWideString Remark;
30         public int FloorNumber;
31         public TMyLongNameWideString CustomField1;
32         public TMyLongNameWideString CustomField2;
33         public TMyLongNameWideString CustomField3;
34         public TMyLongNameWideString CustomField4;
35         public TMyLongNameWideString CustomField5;
36         public TMyLongNameWideString CustomDict1;
37         public TMyLongNameWideString CustomDict2;
38         public TMyLongNameWideString CustomDict3;
39         public TMyLongNameWideString CustomDict4;
40         public TMyLongNameWideString CustomDict5;
41         public bool HasPhoto;
42         public bool IsLose;
43         public bool IsStop;
44         public bool Reserve;
45     }
46 
47     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
48     public struct TMyNameWideString
49     {
50         [MarshalAs(UnmanagedType.LPWStr, SizeConst = 50)]
51         public string Names;
52     }
53 
54 
55     /// <summary>
56     /// 
57     /// </summary>
58     /// 
59     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
60     public struct TMyLongNameWideString
61     {
62         [MarshalAs(UnmanagedType.LPWStr, SizeConst = 50)]
63         public string Names;
64     }
65 
66 
67     [StructLayout(LayoutKind.Sequential)]
68     public struct SYSTEMTIME
69     {
70         public int wYear;
71         public int wMonth;
72         public int wDayOfWeek;
73         public int wDay;
74         public int wHour;
75         public int wMinute;
76         public int wSecond;
77         public int wMilliseconds;
78     }
79 
80 
81 
82 调用:
83  TPersonInfo tPerson = new TPersonInfo();
84 
85             VariantWrapper vv = new VariantWrapper(o);
86                        int iii = client.AddOnePerson(vv);
87 调用时候就出错 了,提示“System.ArgumentException: VariantWrappers 不能存储在 Variant 中。
88    在 System.StubHelpers.ObjectMarshaler.ConvertToNative(Object objSrc, IntPtr pDstVariant)”
View Code

有谁做过类似项目吗?怎么解决??

开国鸟的主页 开国鸟 | 菜鸟二级 | 园豆:232
提问于:2014-05-21 16:27
< >
分享
最佳答案
0

估计是.net com interop 中的VARIANT封送有问题

收获园豆:100
子清 | 菜鸟二级 |园豆:313 | 2014-05-22 16:08
其他回答(3)
0

int iii = client.AddOnePerson(tPerson);

收获园豆:50
Launcher | 园豆:45050 (高人七级) | 2014-05-21 17:17

这个试过了,会提示“值不在预期的范围内”

支持(0) 反对(0) 开国鸟 | 园豆:232 (菜鸟二级) | 2014-05-21 18:12

@开国伟人: wchar_t Names[20] 改成这样:

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
public string Names;

支持(0) 反对(0) Launcher | 园豆:45050 (高人七级) | 2014-05-22 08:59
0

public int PersonnelID; 这个是不是应该用long类型呀

然后,int iii = client.AddOnePerson(tPerson);

收获园豆:50
于为源 | 园豆:956 (小虾三级) | 2014-05-22 08:42

基本上和这个没关系

支持(0) 反对(0) 开国鸟 | 园豆:232 (菜鸟二级) | 2014-05-22 09:35

@开国伟人: 这个最后咋解决的

支持(0) 反对(0) 于为源 | 园豆:956 (小虾三级) | 2014-05-23 16:35
0

二代身份证验证机具的OCX,好多厂商说明书不明白,测了好久才通过

段少卿 | 园豆:199 (初学一级) | 2014-05-22 11:44

能否说明一下,应该怎么弄?

你做的那个有传递结构参数吧?并且C++里面使用的是variant类型。

支持(0) 反对(0) 开国鸟 | 园豆:232 (菜鸟二级) | 2014-05-22 12:05
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册