2010-10-01 104 views
1

好的,所以我有一個編譯爲DLL文件的C++項目。我能夠在C#中引用此文件,並查看/使用DLL文件中的所有對象和函數。我需要做的是通過VB6引用這些對象和功能。如何在不通過COM的情況下從VB6調用C++ DLL?

C++代碼沒有任何內容,看起來像是在創建一個DLL。沒有'__declspec(dllexport)'修飾符,只是C++代碼。

有對象是這樣的:

String^
Array^ 

我不能完全確定它們是什麼。我對C++的瞭解不是非常廣泛,我只是用C++編寫Linux系統,儘管從使用它們看起來像指針。這些對象與C++中的DLL有什麼關係?

無論如何,我可以添加任何我需要的包裝或添加定義文件(.def),但我不知道使用什麼包裝,但我不知道定義文件如何工作或需要如何工作建。

任何幫助和/或建議表示讚賞。如果你也可以向我推薦一些好的信息,那會很有幫助。我所做的所有搜索都沒有幫助。

請記住,我需要從VB6訪問此C++ DLL中的所有函數和對象。

謝謝。

編輯:添加.h文件中,並AssemblyInfo.cpp規範的問題

我在這些文件中改變了一些名字,但結構是一樣的。請注意,這引用了其他文件,但我認爲如果可以使其他人可以工作,則其他人可以使用相同的過程。我可以看到每一個對象,只是沒有方法:

//myDBObject.h 
#pragma once 
using namespace System; 
namespace myDBNamespace { 

#include "ProblemSolution.h" 

public ref class MyDataBaseAccessor 
{ 
public: 
    MyDataBaseAccessor(); 

    static String^ GetServiceVersion() { return sDLLVersion;}; 
    int     GetServiceStatus() { return myiDBStatus;}; 
    String^    GetMyVersion(); 
    String^    GetDBVersion(); 
    String^    GetDLLVersion(); 
    String^    GetExpireDate(); 

    MyOtherObject^  GetMyOtherObject(); 

    int    ProcessProblem(ProblemSolution^dsps); 

private: 
    static MyDataBaseController^myDataBase; 
    static MyOtherObject^  myObjs; 
    static MyDataset^ myDS; 
    static String^    myDBPath; 

    static String^    sDLLVersion = "0.01"; 
    static String^    sReqDBVer = "0.01"; 
    static int      myiDBStatus; 
    static bool     myBoolean, myOtherBoolean, mybNoChain; 

}; 
} 

這裏是AssemblyInfo.cpp文件:

#include "stdafx.h" 

using namespace System; 
using namespace System::Reflection; 
using namespace System::Runtime::CompilerServices; 
using namespace System::Runtime::InteropServices; 
using namespace System::Security::Permissions; 

// 
// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information 
// associated with an assembly. 
// 
[assembly:AssemblyTitleAttribute("My Product Title")]; 
[assembly:AssemblyDescriptionAttribute("")]; 
[assembly:AssemblyConfigurationAttribute("")]; 
[assembly:AssemblyCompanyAttribute("My Company")]; 
[assembly:AssemblyProductAttribute("My Product Name")]; 
[assembly:AssemblyCopyrightAttribute("My Copyright")]; 
[assembly:AssemblyTrademarkAttribute("My Program")]; 
[assembly:AssemblyCultureAttribute("")]; 

// 
// Version information for an assembly consists of the following four values: 
// 
//  Major Version 
//  Minor Version 
//  Build Number 
//  Revision 
// 
// You can specify all the value or you can default the Revision and Build Numbers 
// by using the '*' as shown below: 

[assembly:AssemblyVersionAttribute("1.0.*")]; 

[assembly:ComVisible(true)]; //Here is the ComVisible tag. It was false and I set it to true 

[assembly:CLSCompliantAttribute(true)]; 

[assembly:SecurityPermission(SecurityAction::RequestMinimum, UnmanagedCode = false)]; 

回答

1

使用regasm從您的C++/CLI程序集中創建一個COM類型庫(.TLB)文件。

您現在應該可以通過引用VB6代碼中的TLB文件來引用C++/CLI代碼。從那裏,例如:

最簡單的形式,你可以這樣做:

REGASM MyAssembly.dll程序

現在,所有的COM 兼容類都註冊 作爲COM對象。你可以火起來VB6 並開始編寫代碼:

Dim net As Object 

Set obj = CreateObject("NETProject.Foo") 
obj.Move 

很容易。除了你遲到 綁定,因爲你沒有COM 類型庫。

沒問題! REGASM可以產生 類型庫,你甚至註冊它:

REGASM MyAssembly.dll程序 /tlb:MyAssembly.tlb現在

,在VB6您可以添加引用 類型庫和早期使用 結合:

Dim net As Foo 

Set obj = New NETProject.Foo 
obj.Move 

編輯: 使類COM可見這樣的:

[ComVisible(true)] 
public ref class MyDataBaseAccessor 
+0

試過了。我能夠引用它,但我看不到任何對象或方法。 – 2010-10-01 22:58:19

+0

@Mike - 您應該能夠在OLEVIEW.EXE中查看TLB文件 - 您是否看到要訪問的內容? – 2010-10-01 23:01:29

+0

@Steve - 在oleview中,除了UUID,版本等,TLB文件是空的。聲明'庫MyObject {}'是空的。 – 2010-10-04 15:49:46

2

我不能完全確定它們是什麼。

您不是在查看C++代碼,而是在C++/CLI代碼中查找託管.NET平臺的語言。這也是爲什麼你可以在C#中輕鬆使用這個DLL的原因。

老實說,如果你想在Visual Basic 6中使用託管對象,COM Interop是你最好的選擇。要麼創建一個包裝器,要麼通過COM Interop直接公開包含在DLL中的對象。

編輯:

基本上,你通過使用屬性公開對象,即你的屬性註釋你的類型的源代碼。這些都記錄在這裏:System.Runtime.InteropServices

看一看: Introduction to COM Interop

此外,COM互操作的「聖經」是這本書由亞當森:.NET and COM: The Complete Interoperability Guide

還有處理如何文章公開COM對象並在VB6中使用它:http://www.15seconds.com/issue/040721.htm

+0

好的,我該怎麼做?完全在這個黑暗中,因爲我從來沒有做過COM互操作編程,更不用說C++了。如何包裝對象/功能?我如何使用interop公開DLL? – 2010-10-01 22:18:43

相關問題