-
January 18th, 2011, 09:35 AM
#1
Window in a separate thread
Hi.
I need to create a DLL which exports 2 functions:
a) the function to create a new thread with window
b) the function to terminate that thread (and we need to destroy it's window)
with using WINAPI (MS VC++ 6).
The reason of my issue that DLL should be attached to main application which can stay busy for a long time and this application are not responding to user's activity, but if there stays at least one active window, the user would not be confused.
I'm not C programer, so please be patient.
Here code that I wrote using google, msdn, etc. A problem that I can't create the window (the handle is NULL) so I need you mature advice. And 2nd question is what I need to terminate the child thread by calling function from the main thread (because of peeking windows messages I think I can't use semaphore... )?
Code:
#include "stdafx.h"
#include "windowsx.h"
HANDLE sem1;
//-----------------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wparam,LPARAM lparam)
{
if (Message == WM_DESTROY )
{
PostQuitMessage(0);
return 0;
}
if (Message = WM_PAINT)
{
HDC hdc ( GetWindowDC(hwnd) );
CPen pen(PS_SOLID,4,RGB(255,0,0));
SelectObject(hdc,pen);
Ellipse(hdc,0,0,100,100);
}
return DefWindowProc(hwnd,Message,wparam,lparam);
}
//-----------------------------------------------------------------------------------------------
DWORD WINAPI doSometing(LPVOID p)
{
MSG msg;
HWND hWnd;
HINSTANCE *hInst = (HINSTANCE*) p;
WNDCLASS w;
memset(&w,0,sizeof(WNDCLASS));
w.style = CS_HREDRAW | CS_VREDRAW;
w.lpfnWndProc = WndProc;
w.hInstance = *hInst;
w.hbrBackground = GetStockBrush(WHITE_BRUSH);
w.lpszClassName = "C Windows";
RegisterClass(&w);
hWnd = CreateWindow( "C Windows", "C Windows", WS_OVERLAPPEDWINDOW,
10,10,600,480,NULL,NULL,NULL,NULL);
if (!hWnd)
throw 1;
ShowWindow(hWnd,SW_SHOWNORMAL);
UpdateWindow(hWnd);
while(GetMessage(&msg, hWnd, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
/* while( dwWaitResult != WAIT_OBJECT_0 )
{
dwWaitResult = WaitForSingleObject(
sem1, // handle to semaphore
0L // zero-second time-out interval
);
Sleep(1000);
}*/
return 0;
}
//--------------------------------------------------------------------------------------------------------
extern "C" __declspec(dllexport) void StartInfoThread(HINSTANCE hInst)
{
HANDLE hThread1;
DWORD dwGenericThread;
sem1 = CreateSemaphore(NULL,1,1,NULL);
hThread1 = CreateThread(NULL,0,doSometing,&hInst,0,&dwGenericThread);
}
extern "C" __declspec(dllexport) void StopInfoTread(void)
{
ReleaseSemaphore(sem1, // handle to semaphore
1, // increase count by one
NULL); // not interested in previous count
}
//--------------------------------------------------------------------------------------------------------
BOOL APIENTRY _DllMain(HANDLE,DWORD,LPVOID)
{
return TRUE;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|