c++ - 制作一个 VB-dll 并将其加载到 C++ 应用程序中

标签 c++ vb.net dll entry-point getprocaddress

我有一个问题困扰了整整一个星期,但我无法独自解决。我一直在谷歌搜索,并在各种论坛中搜索......我发现了很多“这可能有效”,试过了,但没有,没有成功。如果有人有任何线索,请帮助我!

我从外部来源获得了许多用 VB 编写的类和函数,我需要能够在 C++ 应用程序中使用这些类和函数。我的第一个想法是:没问题,我将 VB 代码转换为一个 dll,然后从我的 C++ 程序中加载它。这比我想象的要难。我的 C++ 程序不是用 Visual Studio 编写的,但为了简单起见,我开始尝试从 Visual Studio C++ 应用程序加载我的 VB dll(用 Visual Studio 2010 编写)。到目前为止,这是我的代码:

VB 代码:DllModule:类库项目

DllModule.vb

Namespace DllModule
  Public Module DllModule

    Public Const DLL_PROCESS_DETACH = 0
    Public Const DLL_PROCESS_ATTACH = 1
    Public Const DLL_THREAD_ATTACH = 2
    Public Const DLL_THREAD_DETACH = 3

    Public Function DllMain(ByVal hInst As Long, ByVal fdwReason As Long,
      ByVal lpvReserved As Long) As Boolean
        Select Case fdwReason
            Case DLL_PROCESS_DETACH
                ' No per-process cleanup needed
            Case DLL_PROCESS_ATTACH
                DllMain = True
            Case DLL_THREAD_ATTACH
                ' No per-thread initialization needed
            Case DLL_THREAD_DETACH
                ' No per-thread cleanup needed
        End Select

        Return True
    End Function

    'Simple function
    Public Function Add(ByVal first As Integer, ByVal sec As Integer) As Integer
        Dim abc As Integer
        abc = first + sec
        Return abc
    End Function
  End Module
End Namespace

DllModule.def

NAME DllModule
LIBRARY DllModule
DESCRIPTION "My dll"
EXPORTS DllMain @1
        Add @2

C++ 代码:TryVbDllLoad:控制台应用程序

TryVbDllLoad.cpp

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <strsafe.h>

extern "C" {
 __declspec(dllimport) int __stdcall Add(int, int);
}

typedef int (__stdcall *ptf_test_func_1_type)(int, int);

int __cdecl _tmain(int argc, _TCHAR* argv[])
{
    HINSTANCE hdll = NULL;

    hdll = LoadLibrary("DllModule.dll");        // load the dll
    if(hdll) {
        ptf_test_func_1_type p_func1=(ptf_test_func_1_type)GetProcAddress(hdll,"Add");

        if(p_func1) {
           int ret_val = (*p_func1)(1, 2);
        } else {
        DWORD dw = GetLastError();
        }

        FreeLibrary(hdll);              // free the dll
    } else {
        DWORD dw = GetLastError();
    }

    return 0;
}

我可以加载 dll,但 GetProcAddess 返回 NULL,错误代码为 127(找不到指定的过程)。

我试图从 VB 应用程序加载 dll。这有效(即使没有 .def 文件)。但我猜测没有创建 C++ 应用程序可以使用的正确入口点(当我在 Dependency Walker 中打开 dll 时,我看不到入口点或函数)。我试过使用和不使用“Register for COM interop”编译 VB 代码。

1) 我做错了什么?

2) 如果没有什么好的方法可以妥善解决这个问题,除了创建 dll 之外我还能做什么?有没有其他方法可以在我的 C++ 应用程序中使用 VB 类和函数?

亲切的问候

萨拉



感谢您的回答 Mare!

我的 dll 中一定有某种错误,因为当我尝试使用 regsvr32 注册时,我得到:“模块 C:/tmp/DllModule.dll 已加载,但未找到 DllRegisterServer 的起始地址. 检查 C:/tmp/DllModule.dll 是否是有效的 DLL 或 OCX 文件,然后重试。”

另外,当我使用

#import "C\tmp\DllModule.dll"

我明白了

fatal error C1083:无法打开类型库文件:'c:\tmp\dllmodule.dll'


我查看了教程的链接,但有一个小问题:在所有项目类型中没有“ActiveX DLL”之类的东西可供选择。是的,我有 Visual Studio 2010 Professional(试用版,但仍然有效)。

-- 萨拉

最佳答案

感谢所有的输入。我遇到了另一种方法来解决我的问题,使用多文件程序集而不是我的第一个 dll 方法。

我遵循了这个 HowTo 部分:http://msdn.microsoft.com/en-us/library/226t7yxe.aspx#Y749

VB 代码:DllModule:类库项目

DllModule.vb

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 文件)和命令行编译了这个文件:

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 项目属性中:

  • Common Properties -> Framework and References : 添加 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;
}

DllExport.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 添加到非 visual-studio 项目中,就像普通的 dll 一样:

C++ 代码:LoadDll:非 Visual-Studio 项目(在本例中为代码块)

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 类。最后...:)

关于c++ - 制作一个 VB-dll 并将其加载到 C++ 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10949455/

相关文章:

dll - CTest、CMake 和 MinGW : Executables build, 但无法运行,因为找不到新的 DLL

vb.net - 在 Visual Basic .NET 中访问 MTP/WPD 设备

c++ - CUDA C++ : Expected an expression in kernel. cu 文件

c++ - 函数中int变量初始化失败

regex - 数字位数未知的 VB 模式

.net - VB.NET 中的静态方法实现

java - linux平台JAVA如何调用DLL文件中的API?

c++ - 我们应该将#include 移动到 namespace 吗?

c++ - 如何创建一个给定输入行的数组,其中输入行带有整数,其中第一个整数是长度?

c# - 是否有使用 NUnit 测试复杂函数的通用方法?