Error in runtime Dll linking?
-
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?
-
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?
Hello, the codegurus around the world;) First of all, I don't know the exact answer, but have you ever written C or C++ code?:confused: >> I am new to Windows programming. So, you know C? I mean, why don't you use if-else statement to check if the function returns the right value. 1) Check if FindWindow works fine? 2) If 1) works, go to 3) 3) Check if LoadLibrary works fine? 4) If 3) works, go to 4) 5) Check if GetProcAddress works fine? 6) If 5) works, go t 7) 7) Try to use SetWidnowText(...) I think that any programmer needs this kind of approaches even though we use Java or C++. Anyway, I think that SetWindowText's first arguement passes HWND, not HINSTANCE?:confused: Have a nice day! -Masaaki Onishi-
-
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?
Have you traced through the code yet? You'll see that lpfnDllFunc is NULL. That's because there is no function "SetWindowText". There are "SetWindowTextA" and "SetWindowTextW" for ANSI and Unicode, respectively. Your code is using plain chars, so use the ANSI version. --Mike-- http://home.inreach.com/mdunn/ The Signature, back by popular demand: Buffy. Pajamas.
-
Have you traced through the code yet? You'll see that lpfnDllFunc is NULL. That's because there is no function "SetWindowText". There are "SetWindowTextA" and "SetWindowTextW" for ANSI and Unicode, respectively. Your code is using plain chars, so use the ANSI version. --Mike-- http://home.inreach.com/mdunn/ The Signature, back by popular demand: Buffy. Pajamas.
-
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?
-
FindWindow(A/W) and SetWindowText(A/W) both lives in user32.dll. If you are using FindWindow using static link, why do you need runtime linking for SetWindowText? :confused: