Article View: borland.public.cppbuilder.nativeapi
Article #2554Re: Getting Message with WinMain
From: Remy Lebeau (TeamB)
Date: Mon, 12 Feb 2007 23:24
Date: Mon, 12 Feb 2007 23:24
196 lines
6975 bytes
6975 bytes
"poojo hackma" <poojo.com/mail> wrote in message
news:45d0ea45$1@newsgroups.borland.com...
> CreateWindow requires lpClassName, which can be an ATOM.
>
> RegisterClass() creates an ATOM, but requires a WNDCLASS parameter.
>
> The WNDCLASS structure requires lpClassName.
>
> Now I am stuck in a loop.
You did not read the documentation for CreateWindow() carefully
enough:
lpClassName
Points to a null-terminated string or is an integer atom. If this
parameter is an atom, it must be a global atom created by a previous
call to the GlobalAddAtom function. The atom, a 16-bit value less than
0xC000, must be in the low-order word of lpClassName; the high-order
word must be zero.
If lpClassName is a string, it specifies the window class name.
The class name can be any name registered with the RegisterClass
function or any of the predefined control-class names. For a complete
list, see the following Remarks section. "
So, in this case, you would specify your own class name string in the
WNDCLASS structure, and then pass that same string value to
CreateWindow(). Do not use atoms at all. You need to use WNDCLASS so
that you can register a window procedure to handle the messages.
> I am trying to pop up a notification icon in the taskbar.
Creating a window is a separate operation from adding icons to the
System Tray. The only reason to have a window is if you want to
respond to user actions on the icon, such as mouse clicking.
> wchar_t *clob = title;
> wcscat(clob, L"\n");
> wcscat(clob, message);
> MessageBox(NULL, message, L"Debug", MB_OK);
That can be very dangerous code. You are appending your message
string to a buffer that you do not know the size of. Worse, you are
passing string literals when calling Say(), so you are actually trying
to alter static memory, which is likely to crash the program.
Besides, you are not even using clob for anything, so just get rid of
it altogether, ie:
void Say(const wchar_t* message, const wchar_t* title)
{
#ifdef _DEBUG
MessageBox(NULL, message, title, MB_OK);
#endif
}
Now, with that said, I see you using a lot of wchar_t buffers and
literals, but you are calling APIs that can be defined as either Ansi
or Unicode. You should use Microsoft's TCHAR and LPTSTR defines to
make your code more portable. Try this instead:
void Say(LPCTSTR message, LPCTSTR title)
{
#ifdef _DEBUG
MessageBox(NULL, message, title, MB_OK);
#endif
}
LRESULT CALLBACK SpongeBobWindowProc(HWND hwnd, UINT uMsg, WPARAM
wParam, LPARAM lParam)
{
// process messages as needed ...
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
if( SHGetAutoRunPath(Path) == FALSE )
{
Say(TEXT("No AutoRun path. Exiting."), TEXT("WinMain"));
return 0; // no CF card detected. Exit
}
if( IsProcessRunning(TEXT("wceload.exe")) == TRUE)
{
// exit!
Say(TEXT("Process Is Running. Exiting."),
TEXT("WinMain"));
return 0;
}
LPTSTR c = Path;
buffTail = c;
while( *c != 0 )
{
if( *c == TEXT('\\') )
{
buffTail = CharNext(c);
c = buffTail;
}
else
c = CharNext(c);
}
LPTSTR cmdLine;
LPTSTR exeFile = TEXT("App1.exe"); // Main executable
file
LPTSTR cabFile = TEXT("App1.ARM.CAB"); // CAB file for Main
setup
LPTSTR strKey = TEXT("SOFTWARE\\Apps\\LU App1");
LPTSTR str404 = TEXT("File Not Found");
WNDCLASS cls = {0};
cls.lpfnWndProc = SpongeBobWindowProc;
cls.hInstance = hInstance;
cls.lpszClassName = TEXT("TSpongeBob");
if( !RegisterClass(&cls) )
{
// exit!
Say(TEXT("Cannot register sink window. Exiting."),
TEXT("WinMain"));
return 0;
}
hNdWnd = CreateWindowEx(0, cls.lpszClassName, NULL, 0, 0, 0,
0, 0, NULL, NULL, hInstance, &lParam);
if( !hNdWnd )
{
// exit!
Say(TEXT("Cannot create sink window. Exiting."),
TEXT("WinMain"));
return 0;
}
cmdLine = BuildCmdLine(exeFile);
#ifndef _DEBUG
if( lstrcmpi(lpCmdLine, TEXT("install")) == 0 )
{
// Card has been inserted
SHNOTIFICATIONDATA nd = {0};
HICON hIcon = NULL;
nd.cbStruct = sizeof(SHNOTIFICATIONDATA);
nd.dwID = dwID;
nd.clsid = guid;
nd.npPriority = SHNP_INFORM;
nd.csDuration = 20;
nd.hwndSink = hNdWnd;
nd.hicon = ExtractIconEx(cmdLine, 0, NULL, &hIcon, 1);
nd.pszTitle = szTitle;
if( FileExists(cmdLine) == TRUE )
{
wsprintf(htmlBuf, szFormat, szHtmLink, szTitle,
szHtmMsg, szInputYes, szInputNo);
nd.pszHTML = htmlBuf;
nd.lParam = lParam;
if( SHNotificationAdd(&nd) != ERROR_SUCCESS )
{
// exit!
Say(TEXT("Cannot add notification icon.
Exiting."), TEXT("WinMain"));
DestroyWindow(hNdWnd);
UnregisterClass(cls.lpszClassName, hInstance);
return 0;
}
}
}
#endif
MSG msg;
while( GetMessage(&msg, 0, 0, 0) > 0 )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
DestroyWindow(hNdWnd);
UnregisterClass(cls.lpszClassName, hInstance);
return 0;
}
Gambit
Technical Details
Message-ID:
<45d0f730$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<45d0cc1f@newsgroups.borland.com> <45d0cf87$1@newsgroups.borland.com> <45d0ea45$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,ece89f4506d844ba X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <45d0cc1f@newsgroups.borland.com> <45d0cf87$1@newsgroups.borland.com> <45d0ea45$1@newsgroups.borland.com> Subject: Re: Getting Message with WinMain Date: Mon, 12 Feb 2007 15:24:08 -0800 Lines: 198 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 75.83.34.171 Message-ID: <45d0f730$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1171322672 75.83.34.171 (12 Feb 2007 15:24:32 -0800) Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail