问题描述
我写一个C#应用程序来调用第三方VB6 DLL。我已经加入参考VB6 DLL中的引用 - > COM标签。
I am writing a C# application to call a third party VB6 DLL. I have added reference to the VB6 DLL in the References->COM tab.
在DLL中的特定方法需要一个VB6 UDT(用户定义类型)作为参数。
A particular method in the DLL takes a VB6 UDT (User Defined Type) as a parameter.
作为一个自动生成的结构.NET包装的COM这UDT所示。该结构有许多孩子的UDT /结构的以及类型VBA.Collection成员(如由.NET元数据示出)。
This UDT is shown as a struct in the auto generated .NET wrapper for COM. The struct has lots of child UDTs / structs as well as members of type VBA.Collection (as shown by .NET metadata). It also has regular data types like string, short, double, int, etc.
我在我的C#代码初始化这个结构b
I am initializing this struct in my C# code as:
udtEmployee udtEmpData = default(udtEmployee);
我也试过
I also tried
udtEmpData = new udtEmployee();
如果我不使用默认的或者新的初始化,我无法编译我的C#代码,因为编译器抱怨使用未分配的变量。
If I do not initialize it using default or new, I am not able to compile my C# code, as the compiler complains about use of unassigned variable.
我需要通过这个结构作为参考。我是这样做的:
I need to pass this struct as reference. I am doing it like this:
clsEmployee.SetData(ref udtEmpData);
在呼吁VB6的DLL的这个方法,我得到错误:
While calling this method of the VB6 DLL, I am getting error:
错误:试图读取或写入受保护的内存。这通常是
指示其他内存已损坏。
这是什么原因和如何解决?
What is the reason and what is the solution?
请注意,我不能改变VB6的DLL,因为我没有它的源代码。我使用VS 2005
Note, I can not change the VB6 DLL as I do not have its source code. I am using VS 2005.
编辑1:
下面是一个完整的背景:
Here is a complete background:
有一个本地开发的ERP产品,它支持插件使用VB6开发。它有一个配置文件,其指定要加载附加的DLL的名称。然后,这些插件中显示在ERP应用的菜单。在菜单中单击时,ERP调用名为StartAddOn()这应该是存在于VB6 DLL函数。
There is a locally developed ERP product, which supports add-on development using VB6. It has a configuration file, which specifies the names of add-on DLLs to be loaded. These addons are then displayed in a menu in the ERP application. On menu click, the ERP calls a function with the name StartAddOn() which should be present in the VB6 DLL.
我想开发附加在C#中,所以我开发了一个StartAddOn方法,从而将控制权交给我的.NET的DLL一个简单的VB6插件。
I wanted to develop add-on in C#, so I developed a simple VB6 addon with a StartAddOn method, which in turn passes control to my .NET DLL.
在.NET的DLL使用由ERP公开的业务类,并将数据对象来来回回。 。在.NET的DLL,我添加了一个COM引用由ERP厂商公布的DLL
The .NET DLL uses the business classes exposed by the ERP, and passes data objects to and fro. In the .NET DLL, I have added a COM reference to the DLL published by the ERP vendor.
所以,架构是这样的:
ERP-> VB6附加元件与StartAddOn方法 - > NET DLL->使用由ERP厂商发布的COM DLL和它的数据类(结构/ UDT)的
So the architecture is like this:ERP->VB6 AddOn with StartAddOn method->.NET DLL->uses COM DLL published by the ERP vendor and its data classes (structs / UDTs).
我怎样才能调试内存错误?
How can I debug the memory error?
推荐答案
什么的结构是什么样子?它已经有一段时间,因为我没有任何严重的VB6开发,但一件事上语言之间打电话时被VB6的坚持下我记得有时绊倒了我DWORD对准它的所有结构。因此,举例来说,如果你有在中间混一些字节值,它会插入填充,这样所有的值的连得4字节边界上对齐。考虑以下几点:
What does the struct look like? It's been a while since I did any serious VB6 development, but one of the things I remember tripping me up sometimes when calling between languages was VB6's insistence on dword-aligning all of it's structures. So for example if you have some byte values mixed in the middle, it will insert padding so all the values align on an even 4 byte boundary. Consider the following:
Type MyType
A As Long
B As Byte
C As Long
End Type
在存储器中,将有3个字节的b和之间未使用的空间C.当然,如果C#不执行相同的填充,它可以把你的价值观,可能造成各种混乱的。
In memory, there will be 3 bytes of unused space between B and C. Of course if C# is not performing the same padding, it can throw your values off and cause all sorts of chaos.
通过一些编译器(如C),它有可能设置一个编译器开关使用这种类型的对准的。我不知道C#有类似的事情。如果没有,解决的办法是插入适当大小的一些虚拟领域中的C#尺寸的结构。
With some compilers (such as C) it is possible to set a compiler switch to use this type of alignment. I don't know if C# has anything similar. If not, the solution is to insert some dummy fields of the appropriate size in to your struct on the C# size.
下面是它提供了有关如何VB6对齐的详细信息的文章UDT的:
Here's an article which provides more information about how VB6 aligns UDTs:http://www.developerfusion.com/article/3367/copymemory-and-arrays-proper-use/4/
这篇关于调用从C#一个复杂的用户定义类型(UDT)VB6的DLL函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!