2

I have a .exe program in C++ that only shows a window. How can I convert that .exe program into a .dll file and then how can I access that .dll file with C# so I can show that window. Here is my C++ code:

#include <windows.h>

const char g_szClassName[] = "myWindowClass";

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}
Bram Desmet
  • 21
  • 1
  • 2
  • If you find something converts .exe to .dll or to the original code, let me know, we are going to be rich. In other words edit your title. – 0x90 Apr 20 '13 at 15:10
  • 1
    Wouldn't it be easier just to create the window in C#? – WhozCraig Apr 20 '13 at 15:13
  • I am writing a Direct3D application in C++ and I want to use it for my C# program. – Bram Desmet Apr 20 '13 at 15:33
  • Would you consider creating this as an ActiveX control, you can host that in your C# application. You could also return your created window handle from a function exported from your DLL - you'll need a WinForms app for that: HWND MakeMySpecialWindow(); – Peter R Apr 20 '13 at 15:38
  • Thanks Peter R, I will give it a try. But wich hInstance must I use for my window? – Bram Desmet Apr 20 '13 at 15:52
  • 1
    Your C++ program does nothing. There's nothing at all to port. – David Heffernan Apr 20 '13 at 16:09

1 Answers1

2

Two steps:

  1. If you have a c++ program you can build it as a dll by following this post, assuming you use visual studio.
  2. In order to use a c++ dll within a c# program you need to read:

Community
  • 1
  • 1
0x90
  • 39,472
  • 36
  • 165
  • 245
  • But what to do with the hInstance variable? I need that for initializing my window. – Bram Desmet Apr 20 '13 at 15:36
  • read this as well http://social.msdn.microsoft.com/Forums/en-US/netfxcompact/thread/066a1a5d-b463-4988-9997-ce5e519bf16d/ – 0x90 Apr 20 '13 at 15:37