查看: 1504|回复: 6
|
在 VB.net 写 DLL 然后在 C++.NET 使用问题 !!!
[复制链接]
|
|
本人在 VB.net 写了 DLL 然后在 C++.NET 里头使用,但是总是 call 不到DLL. 请问是什么问题?
以下是本人的 coding:
VB.NET 2005 (DLL class Library) Code

VB File: CryptoUtils.vb
用处: Calling function in Encryption.vb
Compile 了, 然后把它抄到 C:\Crypt.dll
Code:
Public Class CryptoUtils
Public Shared Function Encrypt(ByVal msg As String) As String
Try
Dim sym As New Encryption.Symmetric(Encryption.Symmetric.Provider.Rijndael)
Dim key As New Encryption.Data("*-M(y+P)a5|;ss#")
Dim encryptedData As Encryption.Data
'encryptedData = sym.Encrypt(New Encryption.Data("Secret Sauce"), key) 'Original code
encryptedData = sym.Encrypt(New Encryption.Data(msg), key) 'Edited Code
'Dim base64EncryptedString As String = encryptedData.ToBase64 'Original code
Return encryptedData.ToBase64() 'Edited Code
Catch ex As Exception
Throw New Exception(ex.Message, ex)
End Try
End Function
Public Shared Function Decrypt(ByVal msg As String) As String
Try
Dim sym As New Encryption.Symmetric(Encryption.Symmetric.Provider.Rijndael)
Dim key As New Encryption.Data("*-M(y+P)a5|;ss#")
Dim encryptedData As New Encryption.Data
'encryptedData.Base64 = base64EncryptedString 'Original code
encryptedData.Base64 = msg 'Edited Code
Dim decryptedData As Encryption.Data
decryptedData = sym.Decrypt(encryptedData, key)
'Console.WriteLine(decryptedData.ToString) 'Original code
Return decryptedData.ToString 'Edited Code
Catch ex As Exception
Throw New Exception(ex.Message, ex)
End Try
End Function
End Class
C++ File: try.cpp
用处: Calling DLL function that created by VB
Code:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <tchar.h>
#include <string>
#include <iostream>
using namespace std;
string CallMyDLL(void)
{
/* get handle to dll */
HINSTANCE hGetProcIDDLL = LoadLibrary( _T ("C:\\Crypt.dll"));
if (hGetProcIDDLL == NULL)
{
return "*** LoadLibrary failed *** \n";
} else {
/* get pointer to the function in the dll*/
FARPROC lpfnGetProcessID = GetProcAddress(HMODULE(hGetProcIDDLL), "Encrypt");
/*
Define the Function in the DLL for reuse. This is just prototyping the
dll's function. A mock of it. Use "stdcall" for maximum compatibility.
*/
typedef string (__stdcall * pICFUNC)(string);
pICFUNC MyFunction;
MyFunction = pICFUNC(lpfnGetProcessID);
/* The actual call to the function contained in the dll */
string myReturnVal = MyFunction("Hello");
/* Release the Dll */
FreeLibrary(hGetProcIDDLL);
/* The return val from the dll */
return myReturnVal;
}
}
int main (void) {
string encrypt_msg = CallMyDLL();
//char ttt[] = "try only !!!";
cout << "Encrypted Msg: " << encrypt_msg << endl << "Press any key to quit ..." << endl ;
getchar ();
return 0;
}
在 runtime : Unhandled exception at 0x00000000 in Try_Crypto.exe: 0xC0000005: Access violation reading location 0x00000000.
为什么?
那里错了?
FARPROC lpfnGetProcessID = GetProcAddress(HMODULE(hGetProcIDDLL), "Encrypt"); <--- 是这个问题吗?
谢谢帮忙。
[ 本帖最后由 heng81 于 15-2-2008 05:05 PM 编辑 ] |
|
|
|
|
|
|
|

楼主 |
发表于 15-2-2008 11:34 PM
|
显示全部楼层
没人用过吗?如何解决 Access violation reading location 0x00000000 ????? |
|
|
|
|
|
|
|
发表于 16-2-2008 01:03 AM
|
显示全部楼层
你用 。net compile 出来的 managed dll,
若用 managed c++(c++.net) 可以直接加reference 来用
但你上面c++ 那段是 unmanaged code 来的woh |
|
|
|
|
|
|
|

楼主 |
发表于 16-2-2008 08:36 AM
|
显示全部楼层
原帖由 tensaix2j 于 16-2-2008 01:03 AM 发表 
你用 。net compile 出来的 managed dll,
若用 managed c++(c++.net) 可以直接加reference 来用
但你上面c++ 那段是 unmanaged code 来的woh
如何直接加reference 来用 ???
manage C++ 有什么 rules 要跟?? |
|
|
|
|
|
|
|
发表于 16-2-2008 12:32 PM
|
显示全部楼层
你不是用 visual studio 的 ide吗?
有看到reference 在右手边吗?,那里加一下那个。net dll
然后再import 一下namespace 就可以用了。。
managed C++ 要用 mscorlib。dll。。。
然后, 要 compile 给 CLR 用 /clr 的option 要放。 |
|
|
|
|
|
|
|

楼主 |
发表于 17-2-2008 12:57 AM
|
显示全部楼层
原帖由 tensaix2j 于 16-2-2008 12:32 PM 发表 
你不是用 visual studio 的 ide吗?
有看到reference 在右手边吗?,那里加一下那个。net dll
然后再import 一下namespace 就可以用了。。
managed C++ 要用 mscorlib。dll。。。
然后, 要 compile 给 CLR 用 ...
reference 加到 C++ Project 了。

要如何 import 一下 namespace ????
我的是这样的:
#using <mscorlib.dll>
//compile with: /clr (Right Click -> Project properties -> Common Language Runtime Support (/clr))
#import namespace "CryptoUtils" <---- ERROR
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <tchar.h>
#include <string>
#include <iostream>
using namespace std;
string CallMyDLL(void)
{
return Encrypt("haha");
}
int main (void) {
string encrypt_msg = CallMyDLL();
cout << "Encrypted Msg: " << encrypt_msg << endl << "Press any key to quit ..." << endl ;
getchar ();
return 0;
}
有 error 出现 。。。 如何是好 ??? |
|
|
|
|
|
|
|

楼主 |
发表于 18-2-2008 11:36 AM
|
显示全部楼层
|
|
|
|
|
|
| |
本周最热论坛帖子
|