VC ++包装器DLL [英] A VC++ wrapper DLL
问题描述
我有一个不受管理的第三方DLL(包括带有源代码的Visual C ++ Express项目).我想编写一个托管C ++包装DLL,以便可以使用VB中的DLL.
我已经15年没碰过C了-我的大部分演奏"都在VB中进行.
我有C ++和VB 2010 Express.
我创建了一个新的C ++类库,并将一些代码添加到头文件中(请参见下文).
代码会编译.从VB中可以看到我的属性,但是两个函数(WrapiguanaClose和WrapiguaneCode)不可见.一旦我掌握了基础知识,就会添加更多的属性和函数.
这可能是一个愚蠢的错误-显示我对C ++的过时态度.
谁能告诉我我哪里出问题了?
I have a third party DLL (including a Visual C++ Express project with source code) that is, I think, unmanaged. I Want to write a Managed C++ wrapper DLL so I can use the DLL from VB.
I have not touched C for about 15 years - most of my ''playing'' has been in VB.
I have C++ and VB 2010 express.
I have created a new C++ Class Library and added some code to the header file (see below).
The code compiles. From VB I can see my property, but the two functions (WrapiguanaClose and WrapiguaneCode) are not visible. There will be several more properties and functions added once I can get the basics working.
It is probably a silly mistake - showing how out of date I am with C++.
Could anyone please tell me where I have gone wrong ?
// iguanaIRDLLWrapper.h
#pragma once
// #pragma comment(lib, "iguanaIRDLLWrapper.lib")
#include "Stdafx.h"
#include "iguanaIR.h"
#include "resource.h"
using namespace System;
using namespace System::Reflection;
using namespace System::Windows::Forms;
namespace iguanaIRDLLWrapper {
public ref class igWrap
{
// TODO: Add your methods for this class here.
public:
property int Version
{
int get()
{
return 1;
}
/* void set(int x)
{
_StudRank = x;
}*/
}
// void WrapiguanaIRClose(PIPE_PTR connection);
// void WrapiguanaIRCode(const iguanaPacket pkt);
public : void WrapiguanaClose(PIPE_PTR connection)
/* void iguanaClose(PIPE_PTR connection) */
{
typedef int (__cdecl *igClosePROC)(HANDLE);
HINSTANCE hinstLib;
igClosePROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
// Get a handle to the DLL module.
hinstLib = LoadLibrary(TEXT("iguanaIR.dll"));
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
ProcAdd = (igClosePROC) GetProcAddress(hinstLib, "iguanaClose");
// If the function address is valid, call the function.
if (NULL != ProcAdd)
{
fRunTimeLinkSuccess = TRUE;
(ProcAdd) (connection);
}
// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
}
}
public : void WrapiguanaCode(const iguanaPacket pkt)
/* unsigned char iguanaCode(const iguanaPacket pkt) */
{
typedef int (__cdecl *igCodePROC)(iguanaPacket);
HINSTANCE hinstLib;
igCodePROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
// Get a handle to the DLL module.
hinstLib = LoadLibrary(TEXT("iguanaIR.dll"));
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
ProcAdd = (igCodePROC) GetProcAddress(hinstLib, "iguanaClose");
// If the function address is valid, call the function.
if (NULL != ProcAdd)
{
fRunTimeLinkSuccess = TRUE;
(ProcAdd) (pkt);
}
// Free the DLL module.
}
}
};
}
在此先感谢您的帮助.
John Hadley
Thanks in advance for any help.
John Hadley
推荐答案
我想这是因为用作方法参数的类型没有公开为托管数据类型.
V.
Hi,
I suppose it''s because the types used as parameters of your methods are not exposed as managed data types.
V.
Err-对不起-我理解这些字眼,但听不懂意思.
我尝试过:-
//public:无效WrapiguanaClose(PIPE_PTR连接)
public:无效WrapiguanaClose(HANDLE连接)
结果相同.
如果那不是您的意思,那么我应该包括什么代码来公开我的方法参数?
John Hadley
Err - sorry - I understand the words, but not the meaning.
I tried :-
// public : void WrapiguanaClose(PIPE_PTR connection)
public : void WrapiguanaClose(HANDLE connection)
with the same result.
If that was not your meaning what code should I include to expose my method parameters?
John Hadley
尝试使用IntPtr
而不是PIPE_PTR
或HANDLE
:最后两个是仅在 C ++
上定义的数据类型
另一个技巧:避免在每次调用 DLL 时加载和卸载DLL,这会浪费大量时间.最好的方法是在应用程序启动时仅加载 dll 一次,最后将其卸载.您可以从包装器dll 中导出2个函数来执行此操作,例如bool WrapperInitialize()
和void WrapperUninitialize()
Try usingIntPtr
instead thanPIPE_PTR
orHANDLE
: the last two are datatypes defined only on C++
Another trick: avoid loading and unloading the DLL each time you make a call into it, it''s a big waste of time. The best is to load the dll only once at the application startup and unload it at the end. You could export 2 functions from your wrapper dll to do it, likebool WrapperInitialize()
andvoid WrapperUninitialize()
这篇关于VC ++包装器DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!