CURL Read access violation

I made a CURL Test project to begin testing things out and all went well, but when I tried to move it into my main project I ran into plenty of errors. Now, a fair bit later, I've worked it down to only one, yet I've no idea how to fix it!
Every time I click the submit button (calls the cURL function I get this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Exception thrown: read access violation.
**data** was 0xFF. occurred
//Throws exception at
CURLcode curl_easy_setopt(struct Curl_easy *data, CURLoption tag, ...)
{
  va_list arg;
  CURLcode result;

  if(!data)
    return CURLE_BAD_FUNCTION_ARGUMENT;

  va_start(arg, tag);

  result = Curl_setopt(data, tag, arg);

  va_end(arg);
  return result;
}

I've searched a bit and found nothing cURL specific, and I can not think of what's causing this.
Any and all suggestions and ideas are greatly appreciated. If you need more info (such as different code etc please ask)

My cURL function
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
void postInfo(HWND window)
{
	char usertmp[65], hwidtmp[65], postfinal[130];
	/* Add user to postfinal */
	strcpy(postfinal, "user=");
	GetWindowTextA(window, usertmp, sizeof(usertmp));
	if (strcmp(usertmp, "") == 0 || strstr(usertmp, "  "))
	{
		infoBox("Invalid entry!");
		return;
	}
	strcat(postfinal, usertmp);
	strcat(postfinal, "&");
	/* Add HWID to postfinal */
	char szFileSys[255],
		szVolNameBuff[255];
	DWORD dwSerial;
	DWORD dwMFL;
	DWORD dwSysFlags;
	int error = 0;
	GetVolumeInformationA("C:\\", szVolNameBuff,
		255, &dwSerial,
		&dwMFL, &dwSysFlags,
		szFileSys,
		255);
	snprintf(hwidtmp, sizeof(hwidtmp), "%lu", dwSerial);
	strcat(postfinal, "hwid=");
	strcat(postfinal, hwidtmp);
	strcat(postfinal, "\0");
	/* postfinal is now the string to set to post for cUrl */
	CURL *cUrl;
	CURLcode cCode;
	curl_global_init(CURL_GLOBAL_ALL);
	if (cUrl)
	{
		curl_easy_setopt(cUrl, CURLOPT_URL, "http://jimmyonhentai.x10host.com/script.php");
		curl_easy_setopt(cUrl, CURLOPT_POSTFIELDS, postfinal);
		curl_easy_setopt(cUrl, CURLOPT_WRITEFUNCTION, append_to_string); // set the write callback function
		curl_easy_setopt(cUrl, CURLOPT_WRITEDATA, ptr_string); // to which this pointer is to be passed as the last argument 
		cCode = curl_easy_perform(cUrl);
		if (cCode != CURLE_OK)
			printf("Whoops! Failed to perform! %s\n", curl_easy_strerror(cCode));
		curl_easy_cleanup(cUrl);
	}
	curl_global_cleanup();
	if (strcmp(recd_string.c_str(), "Accepted"))
	{
		infoBox("correct");
	}
	else
	{
		errBox("Failed");
	}
	return;
}
initialise your variables
Line 31:
1
2
3
4
	CURL *cUrl; // This remains uninitialized
	CURLcode cCode; // This remains uninitialized
	curl_global_init(CURL_GLOBAL_ALL);
	if (cUrl)

CURLcode cCode = curl_global_init(CURL_GLOBAL_ALL);

See this example code:
1
2
3
4
5
6
7
CURL *curl = curl_easy_init(); // Note: You need this to initialize your cUrl variable!
if(curl) {
  CURLcode res;
  curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
  res = curl_easy_perform(curl);
  curl_easy_cleanup(curl);
}
from:

https://curl.haxx.se/libcurl/c/curl_easy_setopt.html
Something like this would be more in keeping with the spirit of C++.
Caveat: Not tested; not even compiled.

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
79
80
#include <curl/curl.h>
#include <string>
#include <windows.h>

static int append_to_string( char* recd_data, size_t size, size_t nmemb, void* ptr_string )
{
    const std::size_t nbytes = size * nmemb ;

    if( ptr_string && recd_data && nbytes )
    {
        static_cast< std::string* >(ptr_string)->append( recd_data, nbytes ) ;
        return nbytes ;
    }

    else return 0 ;
}

static std::string do_post( std::string url, std::string post_flds )
{
    const auto curl_handle = curl_easy_init() ; // TO DO: RAII wrap in std::unique_ptr
    if( curl_handle == nullptr ) return "*** error: curl_easy_init() failed."

    char error_buffer[CURL_ERROR_SIZE] = "unknown error" ;
    curl_easy_setopt( curl_handle, CURLOPT_ERRORBUFFER, error_buffer );

    curl_easy_setopt( curl_handle, CURLOPT_URL, url.c_str() );
    curl_easy_setopt( curl_handle, CURLOPT_POSTFIELDS, post_flds.c_str() );

    std::string recd_string ;
    std::string* ptr_string = std::addressof(recd_string) ;
    curl_easy_setopt( curl_handle, CURLOPT_WRITEFUNCTION, append_to_string );
    curl_easy_setopt( curl_handle, CURLOPT_WRITEDATA, ptr_string );

    // set other curl options as required

    const auto status = curl_easy_perform(curl_handle) ;
    curl_easy_cleanup(curl_handle) ;

    if( status == CURLE_OK ) return recd_string ;
    else return std::string( "*** error: " ) + error_buffer ;
}

static std::string get_window_text( HWND window )
{
    char temp[1024]{} ;
    const auto n = GetWindowTextA( window, temp, sizeof(temp) );
    return n > 0 ? temp : "" ;
}

static std::string get_serial( std::string volume = "C:\\" )
{
    char szFileSys[1024]{}, szVolNameBuff[1024]{};
    DWORD dwSerial = 0, dwMFL = 0, dwSysFlags = 0 ;

    GetVolumeInformationA( volume.c_str(),
                           szVolNameBuff, sizeof(szVolNameBuff),
                           &dwSerial, &dwMFL, &dwSysFlags,
                           szFileSys, sizeof(szFileSys) ) ;

    return std::to_string(dwSerial) ;
}

void postInfo( HWND window )
{
    static const std::string url = "http://jimmyonhentai.x10host.com/script.php" ;

    const std::string user = get_window_text(window) ;
    if( user.empty() || user.find( "  " ) != std::string::npos ) errBox("Invalid entry!");
    else
    {
        const std::string post_flds = "user=" + user + "&hwid=" + get_serial() ;

        // curl_global_init(CURL_GLOBAL_ALL); // do this once, in main
        const std::string reply = do_post( url, post_flds ) ;
        // curl_global_cleanup(); // do this once, at the end of main

        if( reply == "Accepted" ) infoBox("correct");
        else errBox("Failed");
    }
}
Topic archived. No new replies allowed.