1
我想從我的vb.net項目中調用一個C++函數,並且我正在嘗試創建一個dll來執行此操作。我之前從未嘗試過,因此根據指南我讀了我用dll.def文件創建了一個dll.dll(在Visual Studio中使用C++),並嘗試將它鏈接到我的VB項目。 Athough我可以建立它沒有任何錯誤壓傷,我也得到不能在vb.net項目中使用C++ dll
「System.Runtime.InteropServices.MarshalDirectiveException」
其他信息:PInvoke的限制:不能返回變體。
我的代碼是這樣的:
dll.h
#define WDL_API __declspec(dllexport)
extern "C" WDL_API int __stdcall wdl(void);
dll.cpp
#include "stdafx.h"
#include "dll.h"
#include <stdio.h>
#include <windows.h>
char var[] = {"a"};
extern "C" WDL_API int __stdcall wdl(void)
{
int i, len1 = sizeof(var);
char sName[100], sAns[10];
FILE *ptr;
errno_t errorCode = fopen_s(&ptr, ".\\file", "wb");
for (i = 0; i<len1 - 1; i++)
fprintf(ptr, "%c", var[i]);
fclose(ptr);
return 0;
}
dll.def
LIBRARY dll
EXPORTS
wdl @1
vbproject
Module Module1
Public Declare Auto Function wdl _
Lib "dll.dll" Alias "wdl"()
Sub Main()
Console.WriteLine("inside vb.net")
wdl()
End Sub
End Module
該代碼似乎是有道理的,但我不明白,如果我失去了一些東西或有某種kind.Any幫助將不勝感激!
謝謝您的回答第一all.When的我添加作爲整數我得到System.DllNotFoundException,我不明白,因爲我已經將該dll添加到項目(項目 - >添加現有的項目)。 至於使用DllImport,你的意思是我應該用「 _ 公共共享函數wdl()作爲整數」替換「Public Declare ... As Integer」行? –
yiannis
不要將該DLL添加到該項目。只要確保該DLL與可執行文件在同一目錄中即可。讓我們在使用'Declare'之前切換到pinvoke。 –
該目錄是問題!我想補充說它不是你說的那樣。現在它正在工作,謝謝你的幫助! – yiannis