控制台和Win32 API程序输出变量地址值
生活随笔
收集整理的這篇文章主要介紹了
控制台和Win32 API程序输出变量地址值
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
控制臺程序;
#include <stdio.h> int main(){int a = 100;char str[20] = "www.daye.com";printf("%#X, %#X\n", &a, str);return 0; }Win32 程序;
/*------------------------------------------------- bobo, 2020 -------------------------------------------------*/#include <windows.h> #include <windowsx.h>LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow) {static TCHAR szAppName[] = TEXT("pointerDemo");HWND hwnd;MSG msg;WNDCLASS wndclass;wndclass.style = CS_HREDRAW | CS_VREDRAW;wndclass.lpfnWndProc = WndProc;wndclass.cbClsExtra = 0;wndclass.cbWndExtra = 0;wndclass.hInstance = hInstance;wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);wndclass.lpszMenuName = NULL;wndclass.lpszClassName = szAppName;if (!RegisterClass(&wndclass)){MessageBox(NULL, TEXT("Program requires Windows NT!"),szAppName, MB_ICONERROR);return 0;}hwnd = CreateWindow(szAppName, TEXT("pointerDemo"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT, CW_USEDEFAULT,NULL, NULL, hInstance, NULL);ShowWindow(hwnd, iCmdShow);UpdateWindow(hwnd);while (GetMessage(&msg, NULL, 0, 0)){TranslateMessage(&msg);DispatchMessage(&msg);}return msg.wParam; }LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {HDC hdc;PAINTSTRUCT ps ;char buffer[65];int a = 100;char str[20] = "www.daye.com";switch (message){case WM_CREATE:return 0;case WM_SIZE:return 0;case WM_RBUTTONDOWN:return 0;case WM_LBUTTONDOWN:hdc = GetDC(hwnd); wsprintf(buffer,"%#X",&a);TextOut(hdc, 100, 20, buffer, 8);wsprintf(buffer,"%#X",str);TextOut(hdc, 200, 20, buffer, 8);return 0;case WM_PAINT:hdc = BeginPaint(hwnd, &ps);EndPaint(hwnd, &ps);return 0;case WM_DESTROY:PostQuitMessage(0);return 0;}return DefWindowProc(hwnd, message, wParam, lParam); }運行;
?
wsprintf(buffer,"%#X",&a);
TextOut(hdc, 100, 20, buffer, 8);
把a的地址按十六進制格式化到buffer;在坐標100、20,輸出buffer的值,輸出長度8;
控制臺程序使用printf輸出,可以自帶格式化;
Win32 使用wsprintf格式化,使用TextOut輸出;
總結
以上是生活随笔為你收集整理的控制台和Win32 API程序输出变量地址值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Win32 API 多线程编程示例 -
- 下一篇: netcore 学习