WPF Exe调用函数&在C ++ DLL中设置变量

时间:2018-01-11 10:38:56

标签: c# c++ dll dllimport lnk2005

我有一个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;
}

有没有办法可以以某种方式设置一个bool&来自C#exe的C ++ DLL中的字符串?

P.S:我是C ++的新手,但拥有相当数量的C#知识。

0 个答案:

没有答案