Retrieving Process ID Of Calling Process.


 

There are different scenarios where you require to know the Process ID of calling process. This might be parent process where your application is running under the context of explorer or you have defined your custom process by calling CreateProcess / CreateProcessA / CreateProcessW.  

I have created a MFC application and used the below code in one of my custom member function.

     DWORD sai = GetCurrentProcessId();

      WCHAR valueconvert[50];

      swprintf_s(valueconvert, 50, L”%d”, sai);

   MessageBox(valueconvert,L”process id is” ,MB_OK);

 

I was unable to typecast the value to UINT which would be the 3rd parameter I am passing inside messagebox function. GetCurrentProcessID would store the value into the variable “sai”, further I am using swprintf_s to write formatted to a string and print the value using messagebox.

Advertisements

Cannot convert const char to lpcwstr


#include <windows.h>

#include

<tchar.h>

#include

<winuser.h>

const

TCHAR sai_classname[]= TEXT("saiwindowcls");

LRESULT CALLBACK wndproc(HWND hwnd, UINT m1,WPARAM wparam, LPARAM lparam)

{

switch

(m1)

{

case

WM_CLOSE: DestroyWindow(hwnd);

break

;

case

WM_DESTROY: PostQuitMessage(0);

break

;

default

: return DefWindowProc(hwnd, m1, wparam, lparam);

}

return

0 ;

}

int

WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

{

//sai1 is the object of wndclassex structure

WNDCLASSEX sai1;

HWND hwnd;

MSG msg;

//registering class

sai1.cbSize = sizeof(WNDCLASSEX);

sai1.style = 0;

sai1.lpfnWndProc = wndproc;

sai1.cbWndExtra = 0;

sai1.cbClsExtra = 0;

sai1.hbrBackground = (HBRUSH)(COLOR_BACKGROUND,COLOR_MENU +2);

sai1.hCursor = LoadCursor(NULL,IDC_ARROW);

sai1.hIcon =LoadIcon(NULL,IDI_APPLICATION);

sai1.hInstance = hInstance;

sai1.lpszClassName = sai_classname;

sai1.lpszMenuName = NULL;

sai1.hIcon = LoadIcon(NULL,IDI_APPLICATION);

if (!RegisterClassEx( &sai1))

{

MessageBox(NULL,L"Unable To Register",L"HAZARD", MB_ICONERROR);

return

0 ;

}

//Creating window

hwnd=CreateWindowEx(WS_EX_CLIENTEDGE,sai_classname,L

"Sai Window",

WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,900,600,

NULL,NULL,hInstance,NULL);

if

(hwnd == NULL)

{

MessageBox(NULL,L

"window creation failes",L"BUMP",MB_ICONHAND);

return

0 ;

}

Notice the above code would not execute if you declare the following code as and get error message as "cannot convert const char to lpcwstr"

Above is the solution for the error message and below is the possible mistakes.

 

const CHAR sai_classname[]= "saiwindowcls";

"window creation failes", ( here you should declare either _T(x) or L "string")

 

 

VS 2005 compiling Win32 Program


 
#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow)
{
    MessageBox(NULL, "sainath musings", "info", MB_ICONASTERISK);
    return 0;
}
The above code will generate error.cannot convert parameter 2 from 'const char [3]' to 'LPCWSTR'
The solution For the above code is simply add Tchar.h. 

#include <windows.h>

#include

<tchar.h>

int

WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,

LPSTR lpCmdLine,

int nCmdShow)

{

MessageBoxW(NULL, _T(

"hi"), _T("Note"), MB_ICONASTERISK);

return 0;

}