Thread overview
Struggling with wchar[] to string conversion
Jan 19, 2021
Stefan
Jan 19, 2021
Jack
Jan 19, 2021
Adam D. Ruppe
January 19, 2021
Hi,

I am using dmd2.081.1 on windows building a 32 bit executable.

I am trying to find out how many instances of the same program are running. Therefor I use the core.sys.windows.tlhelp32 module.

With the CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) and Process32First/Process32Next I iterate over all processes and for each process I iterate with CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID) and Module32First/Module32Next over all modules of a process.
The Model32First/..Next fill a MODULEENTRY32 structure which contains a szExePath member that is a wchar[260].
Converting this to a string succeeds (compiler does not complain) with
me32.szExePath.text
However, comparing this with the result of thisExePath() (std.file) never succeeds although the two string values when written with writeln() appear to be the same.

This drives me crazy!

Any ideas?

Tschüß,
Stefan
January 19, 2021
On Tuesday, 19 January 2021 at 15:32:12 UTC, Stefan wrote:
> Hi,
>
> I am using dmd2.081.1 on windows building a 32 bit executable.
>
> I am trying to find out how many instances of the same program are running. Therefor I use the core.sys.windows.tlhelp32 module.
>
> With the CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) and Process32First/Process32Next I iterate over all processes and for each process I iterate with CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID) and Module32First/Module32Next over all modules of a process.
> The Model32First/..Next fill a MODULEENTRY32 structure which contains a szExePath member that is a wchar[260].
> Converting this to a string succeeds (compiler does not complain) with
> me32.szExePath.text
> However, comparing this with the result of thisExePath() (std.file) never succeeds although the two string values when written with writeln() appear to be the same.
>
> This drives me crazy!
>
> Any ideas?
>
> Tschüß,
> Stefan

How are you trying to do such version? if member is of wchar[] get the .ptr and so that you have the memory pointer to operate with

import std.stdio;
import core.stdc.stddef : wchar_t;
import core.sys.windows.windows;

void main()
{
    wchar[MAX_PATH] buffer;
    GetModuleFileName(NULL, buffer.ptr, MAX_PATH);
    auto s = LPWSTRToDString(buffer.ptr);
    writeln(s);
}

// try-catch so that we can mark it as nothrow
string LPWSTRToDString(T)(T s) nothrow
if(is(T == LPWSTR) || is(T == wchar_t*) || is(T == const(wchar)*))
{
    import std.conv : to;
    import std.string : fromStringz;

    try {
          return to!(string)(s.fromStringz);
   } catch(Exception e) { // do your error-handling here
           return "";
   }
}
January 19, 2021
On Tuesday, 19 January 2021 at 15:32:12 UTC, Stefan wrote:

> contains a szExePath member that is a wchar[260].
> Converting this to a string succeeds (compiler does not complain) with
> me32.szExePath.text

You need to slice it on length. That default conversion will include all 260 chars.

So you probably need to scan it for zeroes with wstrlen or whatever first.