感谢所有的投入。我遇到了另一种方法来解决我的问题,使用多文件程序集而不是我的第一个 dll 方法。
我遵循了这个 HowTo 部分:http://msdn.microsoft.com/en-us/library/226t7yxe.aspx#Y749 http://msdn.microsoft.com/en-us/library/226t7yxe.aspx#Y749
VB 代码:DllModule:类库项目
DllModule.cs
Imports System.Runtime.InteropServices
Namespace DllModuleNS
Public Class Class1
Public Function ClassAdd(ByRef first As Integer, ByRef sec As Integer) As Integer
Dim abc As Integer
abc = first + sec
Return abc
End Function
End Class
End Namespace
我使用 Visual Studio(以生成 DllModule.dll 文件)和 cmd 行编译此文件:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Vbc.exe /t:module DllModule.vb
(生成 DllModule.netmodule 文件)。
C++ 代码:TryVbDllLoad:控制台应用程序
TryVbDllLoad.cpp
#using <mscorlib.dll>
#using ".\..\ClassLibrary1\DllModule.netmodule"
using namespace DllModule::DllModuleNS;
int _tmain(int argc, _TCHAR* argv[])
{
Class1^ me = gcnew Class1();
int a = 1, b = 2;
int xx = me->ClassAdd(a, b);
return 0;
}
在我更改的 TryVBDllLoad 项目属性中:
- 通用属性 -> 框架和参考:添加 DllModule-project 作为参考
- 配置属性 -> C/C++ -> 常规:/clr 标志设置
- 配置属性 -> 链接器 -> 输入:将模块添加到程序集设置为 DllModule.netmodule 的路径(/ASSEMBLYMODULE:"DllModule.netmodule")
这导致我可以在 VC++ 代码中使用 VB 类 Class1!
问题解决了!
现在我更进一步,将 TryVBDllLoad 项目更改为 dll:
- 配置属性 -> 常规:配置类型动态库 (.dll)
- 配置属性 -> 链接器 -> 系统:Windows 子系统 (/SUBSYSTEM:WINDOWS)
TryVbDllLoadClass.h
#ifndef TryVbDllLoadClass_H
#define TryVbDllLoadClass_H
class TryVbDllLoadClass
{
public:
TryVbDllLoadClass();
int Add(int a, int b);
};
#endif // TryVbDllLoadClass_H
TryVbDllLoadClass.cpp
#include "TryVbDllLoadClass.h"
#using <mscorlib.dll>
#using ".\..\ClassLibrary1\DllModule.netmodule"
using namespace DllModule::DllModuleNS;
TryVbDllLoadClass::TryVbDllLoadClass() {}
int TryVbDllLoadClass::Add(int a, int b)
{
Class1^ me = gcnew Class1();
int xx = me->ClassAdd(a, b);
return xx;
}
DLL导出.h
#ifndef DLLEXPORT_H
#define DLLEXPORT_H
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#ifdef __dll__
#define IMPEXP __declspec(dllexport)
#else
#define IMPEXP __declspec(dllimport)
#endif // __dll__
extern "C" {
IMPEXP int __stdcall AddFunction(int);
}
#endif // DLLEXPORT_H
DLLMain.h
#define __dll__
#include "dllExport.h"
#include " TryVbDllLoadClass.h"
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)
{
return 1;
}
TryVbDllLoadClass * my;
IMPEXP int __stdcall AddFunction(int first, int second)
{
my = new TryVbDllLoadClass();
int res = my->Add(first, second);
delete my;
return res;
}
然后我可以像普通 dll 一样将该 dll 添加到非 Visual Studio 项目中:
C++ 代码:LoadDll:非 Visual-Studio 项目(本例中为 CodeBlock)
main.cpp
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "dllExport.h"
typedef int( * LPFNDLL_CREATE)(int, int);
HINSTANCE hDLL;
LPFNDLL_CREATE func;
using namespace std;
int main()
{
cout << "Hello world!" << endl;
int key = 35;
hDLL = LoadLibrary("TryVbDllLoadClass.dll");
if(hDLL)
{
cout << "Loaded: " << hDLL << endl;
func = (LPFNDLL_CREATE) (GetProcAddress(hDLL, "_AddFunction@4"));
if(func != NULL)
{
cout << "Connected: " << func << endl;
cout << "Function returns: " << func(key, key) << endl;
}
else cout << " ::: fail: " << GetLastError() << endl;
FreeLibrary(hDLL);
cout << "Freed" << endl;
}
else cout << " ::: fail: " << GetLastError() << endl;
printf("-> Goodbye world!\n");
return 0;
}
这样我就可以使用在 Visuabl Studio 之外创建的现有 C++ 项目中提供的 VB 类。最后...:)