Results 1 to 5 of 5

Thread: Consuming a C# dll with dllexports using QLibrary

Hybrid View

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

    Default Consuming a C# dll with dllexports using QLibrary

    I have an issue using function from a DLL written in C#. The functions are exported using DLLExport using stdcall. Functions without variables work fine. However, a function with a variable corrupts memory and I can't figure out why.

    The DLL code is

    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. }
    8.  
    9. [DllExport("SayHello", CallingConvention = CallingConvention.StdCall)]
    10. public static string SayHello([MarshalAs(UnmanagedType.LPWStr)] string pMessage)
    11. {
    12. return (pMessage);
    13. }
    To copy to clipboard, switch view to plain text mode 

    My QT code is

    Qt Code:
    1. int Testptr=0;
    2. int SayHelloptr=0;
    3.  
    4. typedef int (__stdcall *MyPrototype0)();
    5. MyPrototype0 myFunction0 =
    6. (MyPrototype0) QLibrary::resolve("NFX.Trading.Panel.Bridge.dll", "Test");
    7. if (myFunction0)
    8. Testptr = myFunction0();
    9. std::string TestStr = globalclass.mql4_ansi2unicode(Testptr);
    10.  
    11.  
    12.  
    13. static std::string sHello = "Hi There";
    14. typedef long int (__stdcall *MyPrototype1)(std::string pMessage);
    15. MyPrototype1 myFunction1 =
    16. (MyPrototype1) QLibrary::resolve("NFX.Trading.Panel.Bridge.dll", "SayHello");
    17. if (myFunction1)
    18. SayHelloptr = myFunction1((std::string)sHello);
    19. std::string SayHelloStr = globalclass.mql4_ansi2unicode(SayHelloptr);
    To copy to clipboard, switch view to plain text mode 

    When I call Test(), it returns "The quick brown fox jumps over the lazy dog."

    However, when I call SayHello(sHello), the returned string which should be "Hi There" is garbled.
    Please note the globalclass.mql4_ansi2unicode simply converts the return string pointer into a local string and I know this function works due to the fact that Test() works and the correct string is returned.

    Can anyone point me to my problem?
    Thanks

    Based on Qt 5.7.0 (MSVC 2013, 32 bit)
    Last edited by ebelcher; 3rd August 2016 at 08:35.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Consuming a C# dll with dllexports using QLibrary

    Your function signature in the second case looks wrong as far as I guess from the two different languages.

    Your DLL annotation looks like it says that the C type of the argument should be LPWStr, but your C++ functon signature uses std::string.

    But in your first case you also have "int" as the return type while the C# function has string.
    The variable names make even less sense to me, usually "ptr" indicates a pointer, but an int wouldn't be capable of holding a pointer.

    Maybe Microsoft's developer documentation has some examples on how to call a C# DLL function from C?

    Cheers,
    _

Similar Threads

  1. consuming thread usage.
    By wagmare in forum Newbie
    Replies: 7
    Last Post: 4th February 2013, 11:22
  2. How time consuming is a d-pointer access?
    By pospiech in forum Qt Programming
    Replies: 3
    Last Post: 8th June 2010, 09:54
  3. Application consuming 60% of CPU usage ..
    By wagmare in forum Qt Programming
    Replies: 3
    Last Post: 20th October 2009, 10:23
  4. Once again: buttons and time consuming tasks
    By pampo in forum Qt Programming
    Replies: 1
    Last Post: 4th May 2009, 18:26
  5. Show progress of a time consuming operation
    By rainman110 in forum Newbie
    Replies: 7
    Last Post: 10th February 2008, 12:07

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.