Appending something to buffer.

How do I append the content returned by siSysInfo.dwOemId to nameBuf ?

Could somebody please help me?
Thank you.



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
  // mm.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "user32.lib")

extern "C"
{
	__declspec(dllexport) void DLLFunction(wchar_t* s)
	{
		

		SYSTEM_INFO siSysInfo;

		// Copy the hardware information to the SYSTEM_INFO structure. 

		GetSystemInfo(&siSysInfo);

		// Display the contents of the SYSTEM_INFO structure. 
		//printf("Hardware information: \n");
		//printf("  OEM ID: %u\n", siSysInfo.dwOemId);




		TCHAR nameBuf[MAX_COMPUTERNAME_LENGTH + 2];
		DWORD nameBufSize;

		nameBufSize = sizeof nameBuf - 1;
		if (GetComputerName(nameBuf, &nameBufSize) == TRUE) {

			int len = wcslen(nameBuf); //11;// wcslen(s);
			for (int i = 0; i<len >> 0; i++)
			{
				wchar_t temp = nameBuf[i];
				s[i] = temp;
			}
		}

	}


}

Thanks. But the information at https://msdn.microsoft.com/en-us/library/windows/desktop/bb759987(v=vs.85).aspx is totally out of my knowledge of c++, especially the warning section about security.

I have almost zero knowledge on C++. I am coding my game in unrealscript, and just need a simple dll written in c++ in order for my unrealscript script to retrieve user's computer information from.

In the code above, instead of...
printf(" OEM ID: %u\n", siSysInfo.dwOemId);
I want the content in variable siSysInfo.dwOemId to be appended to variable nameBuf, so that it can output the combined string inside the for...loop.

This is so simple in vb, perl, php using string concatenation. But in c++, it works the diffrent way...

Could someone modify the above code for me please?
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
#include <windows.h>
#include <cstring>
#include <string>

extern "C"
{
    __declspec(dllexport) int GetComputerNameAndOemId( wchar_t* buffer ) // invariant: buffer is large enough
    {
        wchar_t nameBuf[MAX_COMPUTERNAME_LENGTH+2] ;
        DWORD nameBufSize = sizeof(nameBuf) - 1;
        if( GetComputerNameW( nameBuf, &nameBufSize ) == TRUE )
        {
            std::wcscpy( buffer, L"computer name: " ) ;
            std::wcscat( buffer, nameBuf ) ;
            std::wcscat( buffer, L"  oemid: " ) ;

            SYSTEM_INFO siSysInfo;
            GetSystemInfo( &siSysInfo );
            std::wcscat( buffer, std::to_wstring(siSysInfo.dwOemId).c_str() ) ;
            return TRUE ;
        }

        buffer[0] = 0 ;
        return FALSE ;
    }
}

#include <iostream>

int main()
{
    wchar_t str[ MAX_COMPUTERNAME_LENGTH + 128 ] ; // make sure that the buffer is large enough
    if( GetComputerNameAndOemId(str) ) std::wcout << str << L'\n' ;
}

http://rextester.com/IZAWC7787
What you have there is C, not C++, so it's a character array instead of a managed std::string, and so you have notes in the API docs about properly terminating it, etc. More specifically, you have Windows stuff, though sprintf or snprintf would probably still work.

It can of course be done much simpler in C++ with std::string, but your end goal will probably be passing the result through the DLL method, so stick to the simplest of structures. Eventually you may want to convert your method from void to const char* or I guess const wchar_t* in your case.

Edit: didn't notice it was an outgoing parameter -- can keep it void or return a success status as per JLBorges' solution.
Last edited on
Thank you all, very much for all information :)

JLBorges, the code you provided me works well of course.

But I must call GetComputerNameAndOemId() from unrealscript.
When I call GetComputerNameAndOemId() from unrealscript, I get nothing returned.

In order for GetComputerNameAndOemId() in the dll to return something to unrealscript, or modify a certain variable in unrealscript; (one of a few unrealscript-way is that) the function GetComputerNameAndOemId() should have something that looks like what shown in my first thread:

1
2
3
4
5
6
                        int len = wcslen(nameBuf); 
			for (int i = 0; i<len >> 0; i++)
			{
				wchar_t temp = nameBuf[i];
				s[i] = temp;
			}



I have learnt something from your code, and spent many hours trying to modify it with no success.

Basically, in the code of my first thread, I use wcscpy, wcscat:

std::wcscpy( buffer, L"computer name: " ) ;
std::wcscat( buffer, nameBuf ) ;
std::wcscat( buffer, L" oemid: " ) ;

SYSTEM_INFO siSysInfo;
GetSystemInfo( &siSysInfo );
std::wcscat( buffer, std::to_wstring(siSysInfo.dwOemId).c_str() ) ;

and then, use that buffer in a for...loop just like you see in my first thread.

The result is: first time calling, it outputs unexpected text. Second time calling, it crashes with error: Not enough storage is available to process this command...
I don't know what to do now....

Could you please help me one more time by rewriting your code so that it close to the way the code in my first thread works?

..........

Short information on how unrealscript interacts with C++ DLL: https://api.unrealengine.com/udk/Three/DLLBind.html
Last edited on
Well, the documentation states: Under Win32, only stdcall is supported.

So the signature of the function should be modified to:
1
2
3
4
extern "C"
{
    __declspec(dllexport) int __stdcall GetComputerNameAndOemId( wchar_t* buffer ) // invariant: buffer is large enough
    {

http://rextester.com/IZAWC7787


The documentation also states:
When modifying a string out parameter, the new string must be the same length or shorter than the previous value of the string passed in. This can be achieve by initializing the string in UnrealScript to something longer than the maximum value the DLL will store. Failure to do this will likely overwrite memory and cause a crash.


So make sure that you intialise the string in UnrealScript to a long string containing a large number of (say, 128) characters.
"This can be achieve by initializing the string in UnrealScript to something longer than the maximum value the DLL will store."--> I didn't read the documentation carefully :(

Thanks again for all the help.
Topic archived. No new replies allowed.