Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Dll runt time linking

Dll runt time linking

Scheduled Pinned Locked Moved The Lounge
questiontestingbeta-testinghelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mvworld
    wrote on last edited by
    #1

    Hi, I have been trying to use run time linking with Dll files. I am new to Windows programming, so I am confused what is causing the error in my program. I am trying to use run time linking to change the Window Title of a Console widow. Here is the code: #include typedef BOOL (WINAPI *LPFNDLLFUNC) (HINSTANCE hInstance, char *dwText); void main() { HINSTANCE hLibrary; LPFNDLLFUNC lpfnDllFunc; BOOL RetVal; char dwString[] = "Changed Text"; HWND hwnd; char pszWindowTitle[40]; GetConsoleTitle(pszWindowTitle, 40); hwnd = FindWindow(NULL, pszWindowTitle); hLibrary = LoadLibrary("USER32.DLL"); lpfnDllFunc = (LPFNDLLFUNC) GetProcAddress(hLibrary, "SetWindowText"); RetVal = lpfnDllFunc(hLibrary, dwString); } It compiles and links fine, but it performs an illegal operation on running it. On debugging I get the following message: Unhandeled exception in Testing.exe:OxC0000005:Access Violation Any ideas? :) Thanks in advance

    L 1 Reply Last reply
    0
    • M mvworld

      Hi, I have been trying to use run time linking with Dll files. I am new to Windows programming, so I am confused what is causing the error in my program. I am trying to use run time linking to change the Window Title of a Console widow. Here is the code: #include typedef BOOL (WINAPI *LPFNDLLFUNC) (HINSTANCE hInstance, char *dwText); void main() { HINSTANCE hLibrary; LPFNDLLFUNC lpfnDllFunc; BOOL RetVal; char dwString[] = "Changed Text"; HWND hwnd; char pszWindowTitle[40]; GetConsoleTitle(pszWindowTitle, 40); hwnd = FindWindow(NULL, pszWindowTitle); hLibrary = LoadLibrary("USER32.DLL"); lpfnDllFunc = (LPFNDLLFUNC) GetProcAddress(hLibrary, "SetWindowText"); RetVal = lpfnDllFunc(hLibrary, dwString); } It compiles and links fine, but it performs an illegal operation on running it. On debugging I get the following message: Unhandeled exception in Testing.exe:OxC0000005:Access Violation Any ideas? :) Thanks in advance

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      There are a lot of stuff that goes wrong in your program. This is what happens when I run it on my computer: 1. The call to GetConsoleTitle() fails because the window title does not fit into 40 bytes, but you will not notice it since you don't check the return code. 2. The call to FindWindow() fails since there are no valid window name in pszWindowTitle, but you will not notice it since you don't check the return code. 3. The call to LoadLibrary() works fine, but you don't know that since you don't check the return code. 4. The call to GetProcAddress() fails since there are no function called "SetWindowText". It is called "SetWindowTextA" (ANSI) or "SetWindowTextW" (unicode). You don't notice that since you don't check the return code. 5. The call to SetWindowText() fails since the function pointer is invalid (which causes the access violation) AND because parameter 1 is the handle to USER32.DLL instead of a handle to a window. 6. Your program fails because you post your question in the wrong forum on Codeproject... :-) I have changed your program a little, and it should work better now. ------------------------------------------ #include "stdafx.h" #include "windows.h" #include "iostream.h" typedef BOOL (WINAPI *LPFNDLLFUNC) (HWND hWnd, LPCTSTR lpString); void main() { // I don't know how long a window title can be, but pszWindowTitle // must be large enough to hold it. char pszWindowTitle[4096]; char dwString[] = "Changed Text"; DWORD err = GetConsoleTitle(pszWindowTitle, sizeof(pszWindowTitle)); if(err == 0) cout << "Could not get console title\n"; HWND hConsole = FindWindow(NULL, pszWindowTitle); if(hConsole == NULL) cout << "Could not find console window\n"; HINSTANCE hLibrary = LoadLibrary("USER32.DLL"); if(hLibrary == NULL) cout << "Could not load user32.dll\n"; LPFNDLLFUNC lpfnDllFunc = (LPFNDLLFUNC) GetProcAddress(hLibrary, "SetWindowTextA"); if(lpfnDllFunc == NULL) cout << "Could not find function SetWindowTextA()\n"; BOOL RetVal = lpfnDllFunc(hConsole, dwString); if(RetVal == FALSE) cout << "SetWindowTextA() failed\n"; }

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups