Results 1 to 4 of 4

Thread: Moving memory location into QString

  1. #1
    Join Date
    Aug 2016
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Moving memory location into QString

    In my QT project, I'm accessing a DLL which acts as a bridge between my application and another platform. I note first of all that currently all is working very well!

    I have one piece of code that requires me to include some of the standard Windows dlls in my build, and I'd like to get rid of them by using native QT code. The code is below.

    Qt Code:
    1. QString GlobalSettings::mql4_ansi2unicode(quintptr _ptrStringMemory)
    2. {
    3. int szString=0;
    4. uchar *ucValue=NULL;
    5.  
    6. if (_ptrStringMemory == 0)
    7. {
    8. return ("");
    9. }
    10. else
    11. {
    12. szString = lstrlenA((LPCSTR)_ptrStringMemory);
    13.  
    14. ucValue = new uchar[szString+1];
    15. for (int i=0; i< szString+1; i++) // Allow for Null
    16. ucValue[i]=0;
    17.  
    18. RtlMoveMemory((void *)ucValue, (void *)_ptrStringMemory, szString + 1);
    19. QString strptr((const char *)ucValue);
    20. delete [] ucValue;
    21. LocalFree((HLOCAL)_ptrStringMemory);
    22. return (strptr);
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    An example of how this code is used is as follows.

    Qt Code:
    1. QString Bridge::Test()
    2. {
    3. quintptr Testptr=0;
    4.  
    5. typedef int (__stdcall *MyPrototype)();
    6. MyPrototype myFunction =
    7. (MyPrototype) QLibrary::resolve("NFX.Trading.Panel.Bridge.dll", "Test");
    8. if (myFunction)
    9. Testptr = myFunction();
    10. return(globalclass.mql4_ansi2unicode(Testptr));
    11. }
    To copy to clipboard, switch view to plain text mode 

    The function in the DLL returns a string. I need to check the string to see if I'm communicating with the bridge DLL okay.
    While I understand in general what the mql4_ansi2unicode function is doing, I don't know how to do the same thing in QT.

    Can anyone help?
    Thanks

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Moving memory location into QString

    Isn't this exactly the same question you asked 3 months ago here? Or are you now asking how to get rid of the call to mql4_ansi2unicode() by replacing it with something purely Qt?

    This code implies that _ptrStringMemory points to a NULL-terminated C-string (because it is cast to a LPCSTR), but yet the code allocates an unsigned character array, wastes time filling it with zeros, and then replaces those zeros by copying the string into it using RtlMoveMemory. This new unsigned character array is then used to construct a QString but lies to the QString constructor by telling it the unsigned character array argument is really a signed char * pointer argument.

    If _ptrStringMemory is really just a char * pointer (LPCSTR), then just create a QString using the pointer as the constructor argument and return that. No need for all the allocating and copying.

    However, it still looks like _ptrStringMemory is allocated before it is passed to your function, so you will still need to call LocalFree() to avoid a memory leak. Perhaps you could replace this with a call to free() but who knows if Windows and C# will like that.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Aug 2016
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Moving memory location into QString

    Thanks for the reply,

    It is a similar question that I asked a while back yes, but that issue was solved. I'm now trying to get rid of mql4_ansi2unicode() entirely. The QT application is calling a function in a C# dll that has all it's exports exported as CallingConvention.StdCall.

    Qt Code:
    1. [DllExport("Test", CallingConvention = CallingConvention.StdCall)]
    2. public static string Test()
    3. {
    4. string sMessage = "The quick brown fox jumps over the lazy dog.";
    5.  
    6. return (sMessage);
    7. }
    To copy to clipboard, switch view to plain text mode 

    In my calling code the function is typedef'd as
    Qt Code:
    1. typedef int (__stdcall *MyPrototype)();
    To copy to clipboard, switch view to plain text mode 

    So, the dll function assigns a block of memory and passes the address back (in the form of an int) which I then need to read and then free. Sounds simple enough, and if I was working in C, I could see how to do it. However, doing as you suggest ' just create a QString using the pointer as the constructor argument and return that' hasn't worked until now. I just realised in my first atempt some time back, I wasn't casting the pointer properly.

    One last thing. The function mql4_ansi2unicode is used in the other end of the application chain (non QT, but C++) for the following reason.

    //| Lovely function that helps us to get ANSI strings from DLLs to our UNICODE format
    //| http://forum.mql4.com/60708

    As the DLL is written using MC C# and uses type 'string' which is now Unicode and QString is also Unicode by default, I'm assuming that I don't actually need to worry about what this function was originally trying to achieve?

    Thanks for your help. I'll see if I have success eliminating mql4_ansi2unicode entriely.
    Much appreciated.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Moving memory location into QString

    Yes, QString is unicode by default, so you should be able to just assign the string.

    Why do you declare your function prototype as returning "int"? Why not declare it as char * (or more likely wchar_t *) since that's what is really being returned?

    Finally, the string is -not- being allocated in the DLL, so there should be no need to free it on your side. If anything is being allocated on the DLL side, it should be cleaned up by C# during its normal garbage collection. If you do try to free it, it will probably cause stack corruption if not an immediate crash.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 0
    Last Post: 9th October 2012, 21:47
  2. Replies: 3
    Last Post: 13th February 2012, 05:16
  3. Bitmap Memory location to read and draw in Qt
    By augusbas in forum Qt Programming
    Replies: 1
    Last Post: 2nd September 2010, 22:21
  4. dll exception: int at memory location errors
    By jeffpogo in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2009, 21:01
  5. QString memory usage
    By jogeshwarakundi in forum Newbie
    Replies: 1
    Last Post: 13th December 2007, 07:48

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.