-3

I am trying to load a C++ DLL in a C# program, however the variables from C++ when loaded in C# are corrupt (I think)

This is how the output should look like (this is C++)

    //cout << "Loading config, please wait..." << endl;
    LoadConfig();
    Sleep(2000);
    clear();
    cout << "Your current settings are: " << endl << endl;
    cout << "Resolution: " << screen_width << "x" << screen_height << endl;
    cout << "Sensitivty: " << sensitivity << endl;
    cout << "Smoothing: " << smoothing << endl;
    cout << "FOV: " << fov << endl;
    cout << "Mode: " << mode << endl;
    cout << "Color: " << color << endl;
    cout << "MB4: " << mb4 << endl;
    cout << "MB5: " << mb5 << endl;
    cout << "lClick: " << lClick << endl;
    cout << "Hold : " << hold << endl;

    Sleep(10000);

And this is how it actually looks like.


Resolution: 1920x1080
Sensitivty: 1
Smoothing: 1e-07
FOV: 100Your current settings are: 

Mode: 
1Resolution: 
1920x1080Color: PURPLE
Sensitivty: 1

MB4: Smoothing: 1e-071
FOV: 100
MB5: 
0
Mode: 1lClick: 0

Hold : 1Color: PURPLE

MB4: 1
MB5: 0
lClick: 0
Hold : 1
The program '[288] GynLockLoader.exe' has exited with code -1073741510 (0xc000013a).

why does this happen? the variables in C++ are correct, I am loading them thru a config loader LoadConfig();

LoadConfig() (don't mind the messy code, I don't use C++ a lot)

void LoadConfig() {
    ifstream cFile("config.txt");
    if (cFile.is_open())
    {
        string line;
        while (getline(cFile, line))
        {
            line.erase(remove_if(line.begin(), line.end(), isspace),
                line.end());
            if (line.empty() || line[0] == '#')
            {
                continue;
            }
            auto delimiterPos = line.find("=");
            string name = line.substr(0, delimiterPos);
            string value = line.substr(delimiterPos + 1);
            if (name == "resX") {
                screen_width = stoi(value);
            }
            else if (name == "resY") {
                screen_height = stoi(value);
            }
            else if (name == "sensitivity") {
                sensitivity = stod(value);
            }
            else if (name == "smoothing") {
                smoothing = stod(value);
            }
            else if (name == "fov") {
                fov = stoi(value);
            }
            else if (name == "mode") {
                mode = stoi(value);
            }
            else if (name == "color") {
                color = value;
            }
            else if (name == "mb4") {
                if (value == "1") {
                    mb4 = true;
                }
                else {
                    mb4 = false;
                }
            }
            else if (name == "mb5") {
                if (value == "1") {
                    mb5 = true;
                }
                else {
                    mb5 = false;
                }
            }
            else if (name == "lClick") {
                if (value == "1") {
                    lClick = true;
                }
                else {
                    lClick = false;
                }
            }
            else if (name == "hold") {
                if (value == "1") {
                    hold = true;
                }
                else {
                    hold = false;
                }
            }
        }
    }
    else
    {
        cerr << "Couldn't open config file for reading.\n";
    }
}

I call extern C in dllmain.h so I can use main() in C# by calling DllImport

dllmain.h

extern "C"
__declspec(dllexport)
int
main(void);

And this is how I call main() from C#

C# main();

static class Program
    {
        [DllImport("GynLock.dll")]
        public static extern void main();
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Main());
            main();
        }
    }

Everything loads normally, I just don't understand why my output looks so weird and corrupt (?)

Can somebody help clarify this for me, maybe I am missing something basic here...

EngineEye
  • 11
  • 7
  • [Please do not post images of code because they are hard to use.](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) Code should be posted directly **as text** in your question. – MikeCAT Jun 10 '21 at 14:19
  • sorry about that, I fixed it. – EngineEye Jun 10 '21 at 14:26
  • `public static extern void main();` should be `public static extern int main();` by the looks of it – Charlieface Jun 10 '21 at 16:33
  • @Charlieface yea, but that doesn't change the output. – EngineEye Jun 10 '21 at 16:43
  • [\[SO\]: How to create a Minimal, Reproducible Example (reprex (mcve))](https://stackoverflow.com/help/minimal-reproducible-example). Please add all the code (not scattered pieces), input data, expected and actual output. – CristiFati Jul 01 '21 at 10:50

1 Answers1

0

c++ force std::cout flush (print to screen)

add (std::)flush to the end of each of it. As You mix it with c# there could be variety of reasons. If You have github project - tell me, I would try to help. Cheers.

cout << "Your current settings are: " << endl << endl << flush;
badasz
  • 101
  • 8