Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
March 26, 2015 Looking for a little help with the win32 headers | ||||
---|---|---|---|---|
| ||||
Hello, just installed D today. I have this code: import std.stdio; import win32.windef; import win32.winbase; void main() { LPSTR lpBuffer; PDWORD lpnSize; int result = GetComputerNameA(lpBuffer, lpnSize); writeln(result); } It passes zeroes to the API, I'm stuck and can't find a suitable sample in the win32 repository here: https://github.com/AndrejMitrovic/DWinProgramming |
March 26, 2015 Re: Looking for a little help with the win32 headers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Belly | On 26/03/2015 5:52 p.m., Belly wrote: > Hello, just installed D today. I have this code: > > import std.stdio; > import win32.windef; > import win32.winbase; > > void main() > { > LPSTR lpBuffer; > PDWORD lpnSize; > int result = GetComputerNameA(lpBuffer, lpnSize); > > writeln(result); > } > > It passes zeroes to the API, I'm stuck and can't find a suitable sample > in the win32 repository here: > https://github.com/AndrejMitrovic/DWinProgramming According to MSDN you need to preallocate the buffer. import std.stdio; import win32.windef; import win32.winbase; void main() { char[] buffer; buffer.length = MAX_COMPUTERNAME_LENGTH + 1; size_t size = MAX_COMPUTERNAME_LENGTH; int result = GetComputerNameA(buffer.ptr, &size); writeln(buffer[0 .. size]); } I haven't tested it. But I think that should work. Also I think writeln will be outputting with a null terminator. So may need to -1 it. https://msdn.microsoft.com/en-us/library/windows/desktop/ms724295%28v=vs.85%29.aspx |
March 26, 2015 Re: Looking for a little help with the win32 headers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Belly | On Thursday, 26 March 2015 at 04:52:23 UTC, Belly wrote:
> Hello, just installed D today. I have this code:
>
> import std.stdio;
> import win32.windef;
> import win32.winbase;
>
> void main()
> {
> LPSTR lpBuffer;
> PDWORD lpnSize;
> int result = GetComputerNameA(lpBuffer, lpnSize);
>
> writeln(result);
> }
>
> It passes zeroes to the API, I'm stuck and can't find a suitable sample in the win32 repository here:
> https://github.com/AndrejMitrovic/DWinProgramming
---
import std.stdio;
import core.sys.windows.windows;
extern(Windows) export
bool GetComputerNameA(LPTSTR lpBuffer, LPDWORD lpnSize);
void main(string[] args)
{
uint len = 1024;
char[] result;
result.length = len;
GetComputerNameA(result.ptr, &len);
result.length = len;
writeln(result);
}
---
tested win32
|
March 26, 2015 Re: Looking for a little help with the win32 headers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jean pierre | Thank you guys, I tried the second reply and it works great! |
March 26, 2015 Re: Looking for a little help with the win32 headers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Belly | No, wait, the first code is even better because it uses the headers so I don't need to declare the API myself and it uses the MAX_COMPUTERNAME_LENGTH define, too, nice! |
March 26, 2015 Re: Looking for a little help with the win32 headers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Belly | On Thursday, 26 March 2015 at 16:46:06 UTC, Belly wrote:
> No, wait, the first code is even better because it uses the headers so I don't need to declare the API myself and it uses the MAX_COMPUTERNAME_LENGTH define, too, nice!
Using this will return only the first 15 characters of the computer name, if longer.
Please use the unicode version of the function GetComputerNameW instead of the ANSI one (GetComputerNameA), because the second one has a known bug, returning always 0 as required buffer size.
import std.stdio;
import std.c.windows.windows;
wstring getComputerName()
{
enum ERROR_BUFFER_OVERFLOW = 111;
uint size;
if (GetComputerNameW(null, &size) == 0 && GetLastError() == ERROR_BUFFER_OVERFLOW)
{
wchar[] buf = new wchar[size];
if (GetComputerNameW(buf.ptr, &size))
return buf[0 .. size].idup;
}
return "Unknown";
}
int main(string[] argv)
{
writeln(getComputerName());
getchar();
return 0;
}
|
March 27, 2015 Re: Looking for a little help with the win32 headers | ||||
---|---|---|---|---|
| ||||
Posted in reply to rumbu | On Thursday, 26 March 2015 at 17:41:37 UTC, rumbu wrote: > On Thursday, 26 March 2015 at 16:46:06 UTC, Belly wrote: >> No, wait, the first code is even better because it uses the headers so I don't need to declare the API myself and it uses the MAX_COMPUTERNAME_LENGTH define, too, nice! > > Using this will return only the first 15 characters of the computer name, if longer. > > Please use the unicode version of the function GetComputerNameW instead of the ANSI one (GetComputerNameA), because the second one has a known bug, returning always 0 as required buffer size. > > import std.stdio; > import std.c.windows.windows; > > wstring getComputerName() > { > enum ERROR_BUFFER_OVERFLOW = 111; > uint size; > if (GetComputerNameW(null, &size) == 0 && GetLastError() == ERROR_BUFFER_OVERFLOW) > { > wchar[] buf = new wchar[size]; > if (GetComputerNameW(buf.ptr, &size)) > return buf[0 .. size].idup; > } > return "Unknown"; > } > > int main(string[] argv) > { > writeln(getComputerName()); > getchar(); > return 0; > } I changed the code to use GetComputerName, so I guess GetComputerNameA is actually used, right? Anyway it's not really important, I'm just playing around with winAPIs to get a feel of things. I'm making a simple health cheat for a game I've written in FreeBasic! I'm going through a nice D2 tutorial here: http://ddili.org/ders/d.en If anyone reading this can save me some time and help me with this: How to declare a byte pattern, for example to pass to WriteProcessMemory? I guess it's uint[] bytes; How do I set it to 0x12345678 ? |
March 27, 2015 Re: Looking for a little help with the win32 headers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Belly | On Friday, 27 March 2015 at 01:27:25 UTC, Belly wrote: > > If anyone reading this can save me some time and help me with this: How to declare a byte pattern, for example to pass to WriteProcessMemory? > > I guess it's uint[] bytes; > How do I set it to 0x12345678 ? Since WriteProcessMemory accepts a LPCVOID (that is const void*), you can use any type of array (provided that you will take care of endianess): uint[] ints = [0x12345678]; ushort[] shorts = [0x1234, 0x5678]; ubyte[] bytes = [0x12, 0x34, 0x56, 0x78]; WriteProcessMemory(processHandle, address, ints.ptr, ints.length * 4 null); WriteProcessMemory(processHandle, address, shorts.ptr, shorts.length * 2, null); WriteProcessMemory(processHandle, address, bytes.ptr, bytes.length, null); |
Copyright © 1999-2021 by the D Language Foundation