Problem with DLL

Sep 11, 2018 at 10:56am
Hi everyone, i have recently created dll file with plan to use InternetOpen in order to make simple web request but when i load it or start it with rundll32, nothing happens.

In case it's standalone exe file web request is made successfully. Also i should mention it doesnt even work when i try to execute it with CreateRemoteThread on DLL_PROCESS_ATTACH event.


Here is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #undef UNICODE
#include <windows.h>
#include <WinInet.h>

#define EXPORT extern "C" __declspec(dllexport)

EXPORT void RequestFunc()
{
	
	HINTERNET hSession = InternetOpen("MyAgent", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
	HINTERNET hConnect = InternetConnect(hSession, "www.google.com", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
	HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", "/test.txt", NULL, NULL, 0, 0, 1);
	HttpSendRequest(hRequest, 0, 0, 0, 0);

	InternetCloseHandle(hSession);
	InternetCloseHandle(hConnect);
	InternetCloseHandle(hRequest);

	
}


BOOL WINAPI DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}
Sep 11, 2018 at 12:26pm
Why don't you set a breakpoint at the beginning of RequestFunc() and step through it with the debugger and see what happens.
Sep 11, 2018 at 12:50pm
Beside what Thomas1965 stated you should check the result of the functions whether they were successful or not. If not output somehow the error using GetLastError(). See:

https://docs.microsoft.com/en-us/windows/desktop/api/wininet/nf-wininet-internetopena
Sep 11, 2018 at 12:51pm
Debugger shows nothing, i put simple MessageBox and it display correctly but only in case when its before HttpSendRequest, if its after HttpSendRequest, nothing pops out.
Sep 11, 2018 at 2:43pm
I've tried something like this but neither of message boxes pops out, it fail at HttpSendRequest but nothing comes back.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#undef UNICODE
#include <windows.h>
#include <WinInet.h>

#define EXPORT extern "C" __declspec(dllexport)

EXPORT void RequestFunc()
{
	
	/*
	HINTERNET hInternetOpen		= InternetOpen("HTTPGET", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
	HINTERNET hInternetConnect  = InternetConnect(hInternetOpen, "www.rottentomatoes.com", INTERNET_DEFAULT_HTTP_PORT,NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
	HINTERNET m_hRequest		= HttpOpenRequest(hInternetConnect,"GET","/index.jsp","HTTP/1.1",NULL, NULL,INTERNET_FLAG_RELOAD | INTERNET_FLAG_EXISTING_CONNECT, 0);
	*/
	MessageBoxA(0, "William", "Krauss", 0);

	// for clarity, error-checking has been removed
	HINTERNET hSession = InternetOpen("MyAgent", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
	HINTERNET hConnect = InternetConnect(hSession, "www.google.com", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
	HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", "/test.txt", NULL, NULL, 0, 0, 1);
	HttpSendRequest(hRequest, 0, 0, 0, 0);
	// close any valid internet-handles 

	InternetCloseHandle(hSession);
	InternetCloseHandle(hConnect);
	InternetCloseHandle(hRequest);
	/*
	InternetCloseHandle(hInternetOpen);
	InternetCloseHandle(hInternetConnect);
	InternetCloseHandle(m_hRequest);
	*/

	
}

DWORD WINAPI RequestThread() {

	

	// for clarity, error-checking has been removed
	HINTERNET hSession = InternetOpen("MyAgent", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
	
	HINTERNET hConnect = InternetConnect(hSession, "www.google.com", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
	HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", "/test.txt", NULL, NULL, 0, 0, 1);
	MessageBox(NULL, "TEST", "TEST", MB_OK);
	if (!HttpSendRequestA(hRequest, 0, 0, 0, 0)) {
		MessageBox(NULL, "FAIL", "FAIL", MB_OK);
	}
	else {
		MessageBox(NULL, "ALL OK", "OK", MB_OK);
	}
		

	
	// close any valid internet-handles 

	InternetCloseHandle(hSession);
	InternetCloseHandle(hConnect);
	InternetCloseHandle(hRequest);

	return 0;
}


BOOL WINAPI DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		CreateRemoteThread(NULL, NULL, RequestThread(), NULL, NULL, NULL, NULL);

	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}
Sep 11, 2018 at 2:53pm
Again: I suggest to use GetLastError() possibly in conjunction with FormatMessage(...) in order to find out what is going wrong:

https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-formatmessage
Sep 12, 2018 at 11:55am
Thank you problem is fixed.
Sep 12, 2018 at 12:03pm
What did you do?
Other people might have the same problem so it would be nice to share your solution
Topic archived. No new replies allowed.