我有一个用c ++编写的dll,我需要在我的c#代码中使用这个dll.搜索后我发现使用P/Invoke可以访问我需要的函数,但这些函数是在类中定义的,并使用非静态私有成员变量.所以我需要能够创建这个类的实例来正确使用这些函数.如何才能访问此类以便创建实例?我一直无法找到办法.
我想我应该注意c ++ dll不是我的代码.
我的项目给出错误..
*无法在DLL"gsdll32.dll"中找到名为"gsapi_new_instance"的入口点.*
尝试使用Ghost-script Interpreter dll'gsdll32.dll'将.pdf转换为图像格式时
即使我尝试将这个dll复制到所有想要的地方,就像在许多论坛中所说的那样
Win\System32或在项目的目录..错误保持不变.. :(
我使用了Ghost-script给出的PDFConvert.cs类,并在我的转换按钮上编写了以下代码:
private void btnConvert_Click(object sender, RoutedEventArgs e)
{
//First We Check whether the Dll is present
if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\gsdll32.dll"))
{
MessageBox.Show("The library 'gsdll32.dll' required to run this program is not present! download GhostScript and copy \"gsdll32.dll\" to this program directory");
return;
}
if (string.IsNullOrEmpty(txtSingleFile.Text))
{
MessageBox.Show("Enter the File name");
txtSingleFile.Focus();
return;
}
else if (!File.Exists(txtSingleFile.Text))
{
MessageBox.Show("The File Does not exists");
txtSingleFile.Focus();
}
else
ConvertPdfToImage(txtSingleFile.Text);
}
Run Code Online (Sandbox Code Playgroud)
和我的ConvertPdfToImage方法如下:
//The Ghost-Script Class Object …Run Code Online (Sandbox Code Playgroud) 长时间读者,第一次海报.有一天,我希望在这里回答问题......
所以它类似于:"无法在dll中找到名为[function]的入口点"(c ++到c#类型转换)
但我似乎无法应用相同的解决方案......
基本上,我写了一个新的方法:
在C++项目的头文件中定义为:
extern "C" {
__declspec(dllexport) bool IsDataValid();
}
Run Code Online (Sandbox Code Playgroud)
在C++项目的源文件中定义为:(仅限signiature)
extern bool __cdecl IsDataValid() {
//function stuf......... returns a bool
}
Run Code Online (Sandbox Code Playgroud)
导入C#项目中的表单C#应用程序:
[DllImport("CarChipSDK_C_Sharp.dll", EntryPoint = "IsDataValid")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsDataValid();
Run Code Online (Sandbox Code Playgroud)
它是从C#表单.cs文件中的同一个地方调用的:
bool isDataValid = IsDataValid();
Run Code Online (Sandbox Code Playgroud)
它返回一个带有消息的异常:
"无法找到DLL'CarChipSDK_C_Sharp.dll'中命名的入口点'IsDataValid()'.
我在从c ++代码生成的.dll上使用了dumpbin.exe和dependency walker,它显示它具有IsDataValid()入口点.
非常感谢所有帮助......
问题解决了!愚蠢的我,这是我现在公司以前的合作社的代码,结果他正在从bin/release文件夹中读取.dll,我正在构建bin/debug文件夹.应该知道的.真诚的道歉.