Author Topic: My contributions on Josť Roca's forum have gone  (Read 703 times)

Pierre Bellisle

  • Newbie
  • *
  • Posts: 9
Re: My contributions on Josť Roca's forum have gone
« Reply #15 on: July 18, 2020, 05:13:22 am »
About x86/debug, agreed, I forgot to set x64/release after some manipulations.
Figured out how to set "Preprocessor Definitions"
CS_OWNDC removed.
All settings are now as you proposed.
All works fine...
This give me a nice configuration to start with.
Yes, I will give a look at the Pierre project again, now I am able to compile it...

I lost a lot of time in some code earlier because the lack of zero initialization...
The more you pay for an error, the more you remember not to do it again...

Now I'm going to play with RtlIntegerToUnicodeString();
The fun is not over yet!

Thank for your suggestions...





« Last Edit: July 18, 2020, 05:23:59 am by Pierre Bellisle »

Pierre Bellisle

  • Newbie
  • *
  • Posts: 9
Re: My contributions on Josť Roca's forum have gone
« Reply #16 on: July 18, 2020, 09:41:06 am »
It's a start, a little one...
Code: [Select]
#include <windows.h>
#include <iostream>
#include <ntsecAPI.h> //"PUNICODE_STRING"
using namespace std;

typedef int(__cdecl* RtlIntegerToUnicodeString)(ULONG, ULONG, PUNICODE_STRING);
const ULONG STATUS_SUCCESS = 0;

// ****************************************************************************

std::wstring  uLongToString(ULONG dwValue, ULONG dwBase) {
    // Base: 2 binary, 8 octal, 10 decimal, 16 hexadecimal

    HMODULE hLib = LoadLibrary(L"NTDLL.DLL");
    if (hLib != NULL)
    {
        RtlIntegerToUnicodeString pProc = (RtlIntegerToUnicodeString) GetProcAddress(hLib, "RtlIntegerToUnicodeString");
        if (pProc != NULL)
        {
            UNICODE_STRING UnicodeString;
            wchar_t wBuffer[33];        //Longuest binary DWORD will be 32 characters
            UnicodeString.Buffer        = wBuffer;
            UnicodeString.Length        = 32;
            UnicodeString.MaximumLength = 32;

            NTSTATUS RetVal = (pProc) (dwValue, dwBase, &UnicodeString);
            if (STATUS_SUCCESS == RetVal) {
                std::wstring wString = wBuffer;
                return wString;
            }
        }
        FreeLibrary(hLib);
    }
    return NULL;
}

// ****************************************************************************

int main()
{
    std::wstring wString;
    wString = L"0x" + uLongToString(15, 16); //Num, Base: 2, 8, 10, 16";
    MessageBoxW(HWND_DESKTOP, (LPCWSTR) &wString, L"RtlIntegerToUnicodeString", MB_OK | MB_TOPMOST);
}
// ****************************************************************************
//

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1771
    • zapsolution
Re: My contributions on Josť Roca's forum have gone
« Reply #17 on: July 18, 2020, 09:48:55 am »
Yes, in C/C++ ALL variables must be initialized, except the static ones.

To initialize a WCHAR string you can do this:

WCHAR zString[MAX_PATH] = { 0 };

or

WCHAR zString[MAX_PATH]; ClearMemory(zString, sizeof(zString));

ClearMemory is a procedure from my Tools.h API helper, aka:
void ClearMemory(IN void* mem, IN long nSize) {
    memset(mem, 0, nSize);
}


Patrice
(Always working with the latest Windows version available...)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1771
    • zapsolution
Re: My contributions on Josť Roca's forum have gone
« Reply #18 on: July 18, 2020, 09:51:42 am »
To convert numeric values to string use these from Tools.h

WCHAR* STRL(IN long N) {
    static WCHAR longstr[33] = {0};
    _ltow_s(N, longstr, 10);
    return (WCHAR*)longstr;
}

WCHAR* STRH(IN long N) {
    static WCHAR hexstr[33] = { 0 };
    swprintf_s(hexstr, strSize(hexstr), L"0x%.8X", N);
    return hexstr;
}

WCHAR* STRF(IN float N) {
    static WCHAR ws[33] = {0};
    swprintf_s(ws, strSize(ws), L"%f", N);
    return (WCHAR*) ws;
}

Patrice
(Always working with the latest Windows version available...)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1771
    • zapsolution
Re: My contributions on Josť Roca's forum have gone
« Reply #19 on: July 18, 2020, 10:07:24 am »
Note about wstring; while it seems easier to use, the use of class adds extra overhead over direct use of the core API.

I first used wstring because it was closer to what could be done with PB's, however now i am trying to use only WCHAR with my own set of helper functions, to enforce granularity and reduce the size of the framework being used. 

For me small code size has always been my moto, except when i have to work with IL or p-code, because then there is nothing i can do to bypass the bloated dot.NET or WinDev WL.
« Last Edit: July 18, 2020, 10:14:31 am by Patrice Terrier »
Patrice
(Always working with the latest Windows version available...)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1771
    • zapsolution
Re: My contributions on Josť Roca's forum have gone
« Reply #20 on: July 18, 2020, 02:12:31 pm »
Cousin,

Make sure to read that one:
http://www.objreader.com/index.php?topic=334.0
Patrice
(Always working with the latest Windows version available...)

Pierre Bellisle

  • Newbie
  • *
  • Posts: 9
Re: My contributions on Josť Roca's forum have gone
« Reply #21 on: July 18, 2020, 08:49:51 pm »
Salut,

Yep, the string manipulation part is not easy at first, no good BSTR equivalent seems to exist.
One thing I asked myself is what is better, cleaner, and give fewer restrictions.
In C++, there is so many ways to do things... It become confusing.
std::wstring looked interesting, still not easy to use when you want to mix it with pointers.
I'll remember the class overhead you mentioned.
WCHAR seems like a good suggestion, I will try to stick with it.

For now, I won't focus on exe size yet, but more on exploring how to do things.
One important thing though, is to have single exe that do not need external MS DLL that may be unavailable on other computers. I like to write pure portable utilities.

Thank for the C/C++ pitfalls link.
I use a TreeviewEditor to store all this stuff and the "Patrice pitfalls" will fit just fine.

C'est en forgeant qu'on devient forgeron...