Reputation: 1440
I've stumped myself with trying to get a managed DLL to work with my WPF application. The idea is to call the function from within the code behind of the WPF MainWindow (so I can get the window handle) and pass that and another pointer which ends up being a pointer of type wchar_t.
I can call into the dll however the WNDProc does not launch, I think it has something to do with the instance of the application or even the handle but I can't put my finger on it. The project builds successfully, just doesn't run the WNDProc.
The idea of the application is to use WPF and call into c++ to start up a server instance that calls into the remote assistance API built into Windows.
Heres what I have:
#pragma once
#include "stdafx.h"
#include <winsock2.h>
#include <tchar.h>
#include "RDP.h"
#pragma warning(disable : 4996)
#pragma warning(disable : 4267)
#pragma comment(lib,"ws2_32.lib")
namespace RDPServerSession
{
RAS::SERVER* s = 0;
HWND MainWindow = 0;
HWND hL = 0;
HINSTANCE hAppInstance = 0;
wchar_t* password;
enum
{
MESSAGE_NOTIFY = WM_USER + 2,
};
public ref class Server
{
public:
void StartServer(System::IntPtr id, System::IntPtr handle)
{
password = reinterpret_cast<wchar_t*>(id.ToPointer());
WSADATA wData;
WSAStartup(MAKEWORD(2, 2), &wData);
CoInitializeEx(0,COINIT_APARTMENTTHREADED);
/*INITCOMMONCONTROLSEX icex = {0};
icex.dwICC = ICC_LISTVIEW_CLASSES | ICC_DATE_CLASSES | ICC_WIN95_CLASSES;
icex.dwSize = sizeof(icex);
InitCommonControlsEx(&icex);*/
//InitCommonControls();
//PrepareDoMatchTable();
hAppInstance = GetModuleHandle(NULL);
WNDCLASSEX wClass = {0};
wClass.cbSize = sizeof(wClass);
wClass.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_PARENTDC;
wClass.lpfnWndProc = (WNDPROC)Main_DP;
wClass.hInstance = hAppInstance;
wClass.hCursor = LoadCursor(0, IDC_ARROW);
wClass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wClass.lpszClassName = _T("CLASS");
RegisterClassEx(&wClass);
/*MainWindow = CreateWindowEx(0,
_T("CLASS"),
ttitle,
WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS |
WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT,
500, 600, 0,LoadMenu(h,_T("MENU_1")), h, 0);*/
ShowWindow(MainWindow,SW_SHOW);
MSG msg;
while(GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return;
}
static void StartS(HWND hh)
{
if (!s)
{
s = new RAS::SERVER;
int ty = 0,po = 0,col = 16;
bool dy = 0;
bool Rev = false;
if (GetKeyState(VK_CONTROL) >> 15)
Rev = true;
s->CreateVirtualChannel(L"test",true,CHANNEL_PRIORITY_MED);
s->Open();
s->SetWindowNotification(hh,MESSAGE_NOTIFY);
vector<int> pids;
vector<wstring> names;
vector<int> ST;
s->GetShareableApplications(pids,names,ST);
RAS::S_INVITATION* inv = s->Invite(0,password,L"group",3);
if (inv)
{
const wchar_t* password = inv->GetTicket().c_str();
}
return;
}
else
{
delete s;
s = 0;
}
}
static LRESULT CALLBACK Main_DP(HWND hh,UINT mm,WPARAM ww,LPARAM ll)
{
switch(mm)
{
case WM_COMMAND:
{
int LW = LOWORD(ww);
if (LW == 100)
StartS(hh);
return 0;
}
case WM_CLOSE:
{
if (s)
StartS(hh);
DestroyWindow(hh);
return 0;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hh,mm,ww,ll);
}
void StopServer() { delete s; }
~Server() { }
};
}
Any thoughts or suggestions on why it the Wndproc does not run?
Cheers.
Upvotes: 1
Views: 712
Reputation: 652
You can use the resource to create the form and use a CreateDialogParam()
Upvotes: 0
Reputation: 16904
The window procedure Main_DP
is associated with the window class CLASS
. But you don't create a window with that class (the code is commented out) so the window procedure isn't used.
Update
You want to handle messages for the existing main window, but you can't associate a new window class with an existing window. Instead, you need to subclass the window. (As noted by Ben Voigt, you shouldn't use the SetWindowLong
technique.)
Once you've subclassed the window you don't need your own message loop. Just return to the WPF code. (And note that it may not be possible to reliably replace WPF's message loop with your own. It may be doing more than a simple TranslateMessage/DispatchMessage
.)
Upvotes: 1