从Visual Studio 2008(C#)调用Visual Basic 6.0库

问题描述:

我正在使用C#从Visual Studio 2008开发一个应用程序,但是我必须调用一个VB 6.0库,该库是普通的dll文件,但不是有效的COM对象,有人知道怎么做吗?

我正在考虑使用此vb6库从VB6创建COM对象,但是我不知道VB 6是否可以创建COM对象?有人知道吗非常感谢任何回复的人.

Dave

I am developing a application from Visual studio 2008 using C#, but I have to call a VB 6.0 library which is normal dll file but not a valid COM object, anyone knows how to do this?

I am thinking to use this vb6 library to create COM object from VB6, but I don''t know whether VB 6 can create COM object or not? Anyone have any idea? Greate thanks to whoever reply.

Dave

基本上,您安装了VB COM组件,然后转到.NET项目的参考==>. 添加参考" => "COM"标签=>从列表中按名称找到您的组件,然后单击确定".将添加参考,并将其正确包装为装配体.单击添加的参考节点,然后浏览以从.NET项目的侧面查看外观.

从这一点出发,您可以立即在.NET代码中使用这些声明.



我只是意识到VB组件不是COM组件.好.
在这种情况下,您需要的是P/Invoke.

首先,检查您实际从VB DLL中导出的内容.使用某些二进制转储,例如Microsoft DUMPBIN.EXE,请参见 http://msdn.microsoft.com/en-us/library/c1h23y6c(v=VS.100).aspx [ http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx [ http://msdn.microsoft.com/zh-我们/library/system.runtime.interopservices.dllimportattribute.callingconvention.aspx [ http://msdn.microsoft.com/zh-CN/library/system.runtime.interopservices.marshalasattribute.aspx [
Basically you install your VB COM component, then you go to your .NET projec''s references => "Add reference" => "COM" tab => find your component by name from the list and hit "OK". The reference will be added, properly wrapped as assembly. Click on the added reference node and browse to see how it looks from the side of the .NET project.

From this point, you can use those declaration in your .NET code immediately.



I just realized the VB component is not a COM component. Good.
In this case what you need is P/Invoke.

First, check up what you actually exports from your VB DLL. Use some binary dump, such as Microsoft DUMPBIN.EXE, see http://msdn.microsoft.com/en-us/library/c1h23y6c(v=VS.100).aspx[^]; the simplest way to run it is using you Visual Studio Tools -> Visual Studio Command Prompt.

If you did not export what you wanted, please go back to your VB DLL project and do it right (I have no idea how).

Now, you need to import it in your .NET project. It is done totally in your source code. You need to use the attribute [DllImport], System.Runtime.InteropServices.DllImportAttribute, see http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx[^]. Use the entry point name you see using DUMPBIN.EXE as a parameter System.Runtime.InteropServices.DllImportAttribute.EntryPoint.

Use the parameter This is called P/Invoke.
Using it can be very simple in simple cases or quite complex in some other cases. See calling conventions: http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.callingconvention.aspx[^]. You may need to use other attribute such as MarshalAsAttribute to match VB parameter types, see http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshalasattribute.aspx[^]. Look at the Microsoft code samples provided on the MSDN pages I provided above (or around).



If you make a case of exported function you fail to P/Invoke, try to ask for help. You will need to provide exact signature of function(s) you''ve exported.

—SA


实际上,不可能获得VB6来创建非COM .DLL.好吧,无论如何都使用普通手段.我似乎回想起,您可以从某种东西上得到一些废话,以得到没有COM的结果库.DLL,但这是很久以前的事情了.

如果您没有经历过类似的事情,那么您就有一个基于COM的.DLL.您要做的就是注册它(使用REGSVR32),然后在您的C#项目中添加一个COM引用.
Actually, it was impossible to get VB6 to create a non-COM .DLL. Well, using normal means anyway. I seem to recall that you could hack the crap out of something to get a resulting library .DLL without COM, but that was a very long time ago.

If you didn''t go through anything like that, you''ve got a COM-based .DLL. All you have to do is register it (using REGSVR32), then add a COM reference to it in your C# project.


我使用Regsvr32注册了DLL,现在我可以从C#引用它.非常感谢您的帮助.谢谢.

戴夫
I registered the DLL use Regsvr32, and I am now be able to reference it from C#. I really appreciated your help. Thanks.

Dave