Community discussion forum

Hosting Control Panel Applets using C#/C++

This is a comment thread discussing Hosting Control Panel Applets using C#/C++
  • 9 years ago

    This thread is for discussions of Hosting Control Panel Applets using C#/C++.

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 4 years ago

    This article is very useful to understand how to call unmanaged function from C#.net. I have to do the same thing for my project but it seems little complex as it is passing user defined data type as an argument to the calling function. I will appreciate help with this.
    Unmanaged C++ function which I am trying to call from C#.net app. is as below: (from the header file)


    static void LibAction(const libString& actionXML, libString& returnXML, libError& libError);


    libSting is a reference to the container class - wraps Standard Teplate Library String type. declared in the header file as below:


    class LIBCLASSEXPORT LibString : public std::string
    {
     typedef std::string STL_STRING;
    public:


     // --- Constructors/Destructor ---
     
     LibString() { ; }
     LibString(const LibString& S);
     LibString(const char* s);
     LibString(char s);
     LibString(istream&);
     LibString(const STLSTRING & S) : STLSTRING(S) { ; }
     virtual ~LibString();



    libError is a reference to the container class which is used for the error handling.


    How do I call this function from C# windows application.


    Below is how I am trying to declare above function in my C# app.


    [DllImport(@"C:\Test\dlls\Lib.dll", EntryPoint="LibAction")]
    public static extern void LibAction(string actionXML, string returnXML, string libError);


    Instead of string I should be passing ref to the libString. I am not sure how do I do that in C#.


    Please help.





  • 4 years ago

    Sorry, I had a comment, but it was a mis-understanding of the original post.

  • 4 years ago

    Hi,


    have also tried to convert the ambigous #define MAKEINTRESOURCEW(i) (LPWSTR)((ULONGPTR)((WORD)(i))) in C++ into C# but with no luck.
    So I compiled a C++ program using MAKEINTRESOURCE and traced it to see what TYPE is really passed to UpdateResource(..) or similar resource accessing functions. What I hav efound that it actaully passes the interger value (Resource ID). For example if you have a Resource with "MY
    RES_ID" with ID 129
    the compiled code actaully just passes the 129 as a WORD data.


    So in C# while doing DllImport, I just chaged the Data Type for the Resource Id parameter which uses MAKEINTRESOURCE.
    Here is what I did with UpdateResource


    [DllImport("kernel32.dll")]
    static extern bool UpdateResource(IntPtr hUpdate, string lpType, int lpName,  ushort wLanguage, IntPtr lpData, uint cbData);


    Notice the Third param which has been chaged from String to int. And It works!!!  Now you will just pass (using my example as mentioned above) 129 instead of MYRESID, or directly MYRESID if it is defined somewhere to reprent 129.





    Rezaul Kabir
    shuvro@yahoo.com

Post a reply

Enter your message below

Sign in or Join us (it's free).