В учебных целях я создал свою первую очень простую DLL Visual C++ и хочу использовать ее в VB.Net.
pch.h
#ifndef PCH_H
#define PCH_H
#include "framework.h"
//#include "Code.h"
#endif //PCH_H
code.h
#pragma once
#include "pch.h"
#include
using namespace std;
#ifndef ADD_CODE_H
#define ADD_CODE_H
//extern "C" //debugger says that IS NOT compatible with the __stdcall declaration
//{
__declspec(dllexport) string __stdcall add_code(string text); //same if I remove __stdcall
//}
#endif
code.cpp
#include
#include
#include "pch.h"
#include "Code.h"
using namespace std;
string __stdcall add_code(string text) { //same if I remove __stdcall
return text;
}
dllmain.cpp
#include "pch.h"
#include "Code.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
Ошибок при компиляции кода Visual C++ нет. Пробовал как x86, так и x64.
Из VB.NET я пытаюсь вызвать DLL с помощью этого кода:
Imports System.Runtime.InteropServices
'' //same if I remove stdcall Convention
Private Shared Function add_code(ByVal text As String) As String
End Function
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim foo As String = add_code("ppp")
End Sub
Пробовал как x86 (dll x86), x64 (dll x64), так и любой процессор (dll x86 или x64).
Я получаю сообщение об ошибке:
System.EntryPointNotFoundException: не удалось найти точку входа с именем «add_code» в DLL
РЕДАКТИРОВАТЬ
@Remy
Я удалил все ссылки на "__stdcall" и раскомментировал код "extern "C" в dll C++, так что теперь функцию следует экспортировать как "_add_code".
Я изменил объявление DLL в:
Private Shared Function _add_code(ByVal text As String) As String
End Function
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim foo As String = _add_code("ppp")
End Sub
Ничего не меняется, я получаю ту же старую ошибку.
EDIT 2
Теперь .dll возвращает целочисленное значение, никаких проблем со строками.
#include
#include
#include "pch.h"
#include "Code.h"
using namespace std;
int add_code(string text) {
switch (text.length()) {
case 3:
return 1;
case 4:
return 2;
case 5:
return 3;
default:
return 9;
}
return 0;
}
Я изменил объявление следующим образом:
Public Shared Function _add_code(ByVal text As String) As Integer
End Function
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim foo As Integer = _add_code("ppp")
End Sub
Ничего не меняется, я получаю ту же старую ошибку, «System.EntryPointNotFoundException».
Я также попробовал сделать дамп, чтобы получить таблицу экспорта с помощью командной строки VS... результат:
Microsoft (R) COFF/PE Dumper Version 14.44.35217.0
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file MyDll.dll
File Type: DLL
Section contains the following exports fory MyDll.dll
00000000 characteristics
FFFFFFFF time date stamp
0.00 version
1 ordinal base
1 number of functions
1 number of names
ordinal hint RVA name
1 0 00001210 add_code = _add_code
Summary
1000 .data
1000 .rdata
1000 .reloc
1000 .rsrc
2000 .text
Подробнее здесь: https://stackoverflow.com/questions/797 ... rom-vb-net