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