如何在通过firebreath生成的托管visual c ++项目中使用外部dll文件

时间:2013-04-25 12:34:52

标签: firebreath

我必须在firebreath项目中使用外部dll文件的一些功能。这个项目是托管c ++项目。我想知道如何在我的项目中引用或包含外部文件。我没有在visual studio 2010中获得Add Reference选项(因为这是一个托管的c ++项目)。请告诉我一个方法来做这个..

1 个答案:

答案 0 :(得分:4)

假设您知道要在DLL中调用的函数的名称,您必须使用的机制如下:

// these are examples of functions --> change return values and params as needed
typedef CHAR (WINAPI *DLL_FUNC1) (USHORT, USHORT);
typedef CHAR (WINAPI *DLL_FUNC2) (USHORT, UCHAR*, UCHAR*, USHORT, UCHAR*, USHORT*, UCHAR*);
typedef CHAR (WINAPI *DLL_FUNC3) (USHORT);

// load library
HMODULE hDLL = LoadLibrary( L"\\path\\to\\your.dll" );

// check if dll was loaded 
if (hDLL == NULL) {
    // error 
    return;
}

// assign functions
DLL_FUNC1 func1 = (DLL_FUNC1) GetProcAddress( hDLL, "name_of_func1" );
DLL_FUNC2 func2 = (DLL_FUNC2) GetProcAddress( hDLL, "name_of_func2" );
DLL_FUNC3 func3 = (DLL_FUNC3) GetProcAddress( hDLL, "name_of_func3" );

// use functions --> here func1 as an example
if( func1( 1, 2 ) != OK ) { // or whatever return value
    // error
    FreeLibrary( hDLL ); 
    return;
}

// --> go on working with the DLL functions

// do not forget to call at the end
FreeLibrary( hDLL );