我有一个 WPF 应用程序,它需要调用用 C++ 编写的 DLL 中的函数。
插图
WPFAppClass.cs (C#)
public class SampleClass
{
[DllImport("SimDll.dll")]
public static extern bool SetLoggingOn(string path);
public void Function(string path) //invoked from a click command
{
SetLoggingOn(path);
}
}
SimDll.dll (C++)
DLLFnc.h
#define DECLARE_DLL __declspec(dllexport)
extern "C" {
BOOL DECLARE_DLL __stdcall SetLoggingOn(CString path);
}
FncDll.cpp(我知道头文件和源文件的名称不同)
BOOL __stdcall SetLoggingOn(CString path)
{
if (!path.IsEmpty())
{
//The issue lies here.
//I need to be able to set a boolean & a path value to be used in
//another class.
//Upon declaring GLOBAL variables & using it I get
//multiply defined symbols error.
//I tried making the variables I want STATIC, code builds, but when
//C# code invokes this method, I get MemoryAccessViolation saying
//the memory being read is protected or corrupt.
//FYI: If I do nothing & just return true or false IT WORKS
return true;
}
return false;
}
有没有办法可以从 C# exe 以某种方式在 C++ dll 中设置布尔和字符串?
PS:我是 C++ 的新手,但有相当多的 C# 知识。