CREATING A DLL-BASED APPLICATION Most DLLs are designed to be used with many applications See previous online notes to see how it’s done Many times your DLL will be designed to be used with one application Let’s see how to do that with Developer Studio Illustrates how to set up multi-project workspaces A workspace can contain several projects The DLLTST workspace DLLCALL (.exe) REVSTR (.dll) Creating the new workspace File / New / Workspaces tab Type in ‘dlltst’ in Workspace Name field Press <Enter> Creating the REVSTR dll project in the workspace File / New / Projects tab Select Win32 Dynamic Link Library Click ‘Add to Current Workspace’ Radio Button Type ‘revstr’ in Project Name field Don’t press ‘OK’ Remove ‘revstr’ from ‘Location’ field so project is in ‘dlltst’ Press ‘OK’, select ‘Empty DLL Project’, ‘Finish’, ‘OK’ Copy revstr.cpp and revstr.h to dlltst directory Add revstr.cpp to project (Project / Add to Project) /Files / revstr.cpp) Build the project Creating the DLLCALL project in the workspace File / New / Projects tab Select Win32 Application (Make sure ‘Add to Current Workspace’ button is checked) Type ‘dllcall’ in Project Name field Remove ‘dllcall’ from ‘Location’ field and ‘OK’, ‘Finish’, ‘OK’ Copy dllcall.cpp, dllcall.h, and dllcall.rc to dlltst directory Add dllcall.cpp and dllcall.rc to project Building the application Project / Set Active Project Select dllcall Project / Dependencies Choose dllcall from the ‘Select Project to Modify’ listbox Check revstr in the ‘Dependent on the following project’ check box Project / Settings / General Tab Change ‘Intermediate Files’ & ‘Output Files’ in right pane to ‘debug’ dir Makes sure revstr.dll is in same directory as dllcall.exe Build the application RUNTIME DYNAMIC LINKING Use: hLib = LoadLibrary(“dll_file_name”); // Load the DLL pFtn = GetProcessAddress(hLib, “ftn_name”); // Get ptr to the function Then call the function by transferring control to its address using pFtn Example: A Roundabout Way of Drawing a Rectangle Rectangle() is a function in the GDI32.DLL dynamic link library Can call it as shown above: Typedef BOOL (WINAPI * PFNRECT) (HDC, int, int, int, int); HINSTANCE hLib; PFNRECT pfnRect; HDC hDC; int xLeft=10, yTop=10, Xright=100, yBottom=100; hLib = LoadLibrary(“GDI32.DLL”); pfnRect = (PFNRECT) GetProcAddress(hLib, “Rectangle”); pfnRect(hDC, xLeft, yTop, xRight, yBottom); FreeLibrary(hLib);