C#C
C#2y ago
Silme94

✅ Using a C# DLL in C++

im actually trying to call a C# dll from c++ with DllExport, the c++ code return no error but dont execute the functions, here is the source :
C# :
using System.Runtime.InteropServices;
using System;


namespace InternalTest
{
    public class Class1
    {
        
        [DllImport("user32.dll")]
        public static extern int MessageBox(int hWnd, string text, string caption, uint type);


        [DllExport(CallingConvention = CallingConvention.StdCall)]
        public static void DllEntryPoint()
        {
            Console.WriteLine("This is a message from C#");
            MessageBox(0, "C# is good", "hello world", 0);
        }
    }
}

C++ :
#include <stdio.h>
#include <Windows.h>

int main(int argc, char **argv) {
    if (argv[1] == NULL) {
        printf("Usage : dll_executor <DLL_PATH>\n");
        return -1;
    }

    char* Dll_Path = argv[1];
    HMODULE hModule = LoadLibraryA(Dll_Path);
    if (hModule == NULL) {
        printf("Failed to load the dll.\n");
    }

    LPCSTR Function_To_Execute = "DllEntryPoint"; // Function to execute in the dll
    
    HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)GetProcAddress(hModule, Function_To_Execute), NULL, 0, NULL);
    if (hThread == NULL) {
        printf("Failed to create thread.\n");
    }

    printf("Dll Executed!\n");
    WaitForSingleObject(hThread, INFINITE);
    CloseHandle(hThread);
    FreeLibrary(hModule);

    return 0;
}

The cpp code says that it got executed but it doesn't really
Was this page helpful?