0

I have a WPF application which needs to invoke a function in a DLL written in C++.

ILLUSTRATION

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 (I'm aware that the header & source files are of different names)

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;
}

Is there a way that I can somehow set a bool & string in C++ dll from C# exe?

P.S: I'm a novice in C++ but have fair amount of C# knowledge.

Ankit
  • 672
  • 6
  • 18
  • what string and boolean are you trying to set? – BugFinder Jan 11 '18 at 10:40
  • String is the parameter that I'm trying to set & boolean to true. The purpose of doing it is to check if boolean is true, then use this parameter (path) to create a file. The creation of file happens in a different class. – Ankit Jan 11 '18 at 10:43
  • 1
    but the boolean yo';re setting isnt in the DLL and youve already got a passing of string ish.. read https://stackoverflow.com/questions/20752001/passing-strings-from-c-sharp-to-c-dll-and-back-minimal-example you surely want to read the boolean returned not set one – BugFinder Jan 11 '18 at 10:45
  • You do not see the boolean & string declared in the DLL. I've mentioned in the comments, 2 ways that I tried declaring them but that dint work. – Ankit Jan 11 '18 at 10:47
  • Ok so did you read passing the string link I gave you? and no you dont set the boolean in the dll thats what it gives you back. – BugFinder Jan 11 '18 at 10:48
  • I just read it. I'll try changing CString to char * & check. Yes the DLL returns a boolean, but also it must set it in a variable for me to be able to use it in another class. – Ankit Jan 11 '18 at 10:52
  • You use it as a return variable just like you do in c# .. its really no different so you could use bool isloggedin= SetLoggingOn(path) ; which gives you your isloggedin result to use whereever or if you need to see it elsewhere you have a public variable in your c# .. i think you're worrying about stuff you dont need to, check https://stackoverflow.com/questions/4608876/c-sharp-dllimport-with-c-boolean-function-not-returning-correctly too – BugFinder Jan 11 '18 at 11:26
  • The link that you shared first has the right solution. I cannot pass string across interop boundary. Changing to const char* did the trick. – Ankit Jan 11 '18 at 15:29

0 Answers0