Visual C/C++ 6.0でSoftAd.dllを動的にリンクする例を示します。SoftAd.dllを静的にリンクした場合は、ユーザーのマシンにSoftAd.dllがないとソフトウェアを起動できませんが、以下のように動的にリンクすると、SoftAd.dllがなくても(広告は表示されませんが)ソフトウェアを起動できるというメリットがあります。
静的にリンクする場合とは異なり、SoftAd.libは使用しません。
#include <windows.h> #include "SoftAd.h" #define FC_EXIT 100 #define FC_AD 200 // 他のメニュー項目と重ならない任意のメニュー識別子 HANDLE hDll = NULL; TSA_Initialize pSA_Initialize = NULL; TSA_Cleanup pSA_Cleanup = NULL; TSA_InitMenuItem pSA_InitMenuItem = NULL; TSA_DrawMenuItem pSA_DrawMenuItem = NULL; LRESULT CALLBACK WndProc( HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam ); int WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, char* cmdlin, int showFlag ) { static char className[] = "HelloClass"; WNDCLASSEX wc; HMENU hMenu; HWND hwnd; MSG msg; memset( &wc, 0, sizeof( wc ) ); wc.cbSize = sizeof( wc ); wc.lpfnWndProc = WndProc; wc.hInstance = hInst; wc.lpszClassName = className; wc.style = CS_HREDRAW|CS_VREDRAW; wc.hCursor = LoadCursor( NULL, IDC_ARROW ); wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH ); RegisterClassEx( &wc ); hMenu = CreateMenu(); InsertMenu( hMenu, 0, MF_BYPOSITION, FC_EXIT, "終了(X)" ); hwnd = CreateWindow( className, "Hello", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, hMenu, hInst, NULL ); ShowWindow( hwnd, showFlag ); UpdateWindow( hwnd ); hDll = LoadLibrary( "SoftAd.dll" ); if ( hDll ) { pSA_Initialize = (TSA_Initialize)GetProcAddress( hDll, "SA_Initialize" ); pSA_Cleanup = (TSA_Cleanup)GetProcAddress( hDll, "SA_Cleanup" ); pSA_InitMenuItem = (TSA_InitMenuItem)GetProcAddress( hDll, "SA_InitMenuItem" ); pSA_DrawMenuItem = (TSA_DrawMenuItem)GetProcAddress( hDll, "SA_DrawMenuItem" ); pSA_Initialize( hwnd, NULL, FC_AD, <SoftID> ); } while ( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } if ( hDll ) { pSA_Cleanup(); FreeLibrary( hDll ); } return msg.wParam; } LRESULT CALLBACK WndProc( HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam ) { LRESULT res = 0; switch( iMsg ) { case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint( hwnd, &ps ); RECT rc; GetClientRect( hwnd, &rc ); DrawText( hdc, "Hello World!", -1, &rc, DT_CENTER|DT_VCENTER|DT_SINGLELINE ); EndPaint( hwnd, &ps ); break; } case WM_COMMAND: { int fc = LOWORD( wParam ); if ( fc == FC_EXIT ) PostQuitMessage( 0 ); break; } case WM_DESTROY: PostQuitMessage( 0 ); break; case WM_MEASUREITEM: if ( hDll ) pSA_InitMenuItem( (MEASUREITEMSTRUCT*)lParam ); break; case WM_DRAWITEM: if ( hDll ) pSA_DrawMenuItem( (DRAWITEMSTRUCT*)lParam ); break; default: res = DefWindowProc( hwnd, iMsg, wParam, lParam ); } return( res ); }
cl /c Hello.c link Hello.obj -SUBSYSTEM:WINDOWS user32.lib gdi32.lib
SoftAd Copyright © Tama Software Ltd.