www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Looking for a little help with the win32 headers

reply "Belly" <vertigo123 gmail.com> writes:
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
Mar 25 2015
next sibling parent Rikki Cattermole <alphaglosined gmail.com> writes:
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
Mar 25 2015
prev sibling parent reply "Jean pierre" <vazyjeanpierre nowhere.uk> writes:
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
Mar 25 2015
parent reply "Belly" <no address.found.com> writes:
Thank you guys, I tried the second reply and it works great!
Mar 26 2015
parent reply "Belly" <no address.found.com> writes:
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!
Mar 26 2015
parent reply "rumbu" <rumbu rumbu.ro> writes:
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; }
Mar 26 2015
parent reply "Belly" <vertigo123 gmail.com> writes:
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 ?
Mar 26 2015
parent "rumbu" <rumbu rumbu.ro> writes:
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);
Mar 26 2015