Calling C++ function pointer from C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zahid313
    New Member
    • Nov 2008
    • 20

    Calling C++ function pointer from C#

    Hi all

    C# code
    ///////////////////////////////////////////////////////////////////////////////////////
    public delegate void EnumDateFormats ProcEx1([MarshalAs(Unman agedType.LPStr)]string xmlString);

    [DllImport("demo dll.DLL")]
    public static extern void GetContentsCall Back(ProcEx callBackProc);

    GetContentsCall Back(Callback1) ;

    public void Callback1(strin g xmlString)
    {
    Console.WriteLi ne(xmlString);
    }
    ///////////////////////////////////////////////////////////////////////////////////////
    C++ code

    demo.h
    extern "C" __declspec(dlle xport) void GetContentsCall Back(void *functocall);

    demo.cpp

    typedef void (*Myfunctocall) (char *xmlString);
    Myfunctocall ptr;
    char *y;

    void GetContentsCall Back(void *functocall)
    {
    ptr = (Myfunctocall)f unctocall;
    std::string resultString = "";
    std::string r1 = "123123";
    for(int i=0;i<20;i++)
    {
    resultString += "<fdsfds>"+r1+" </fdsfds>";
    }
    y = new char[resultString.le ngth() + 1];
    for(int i = 0; i < resultString.le ngth(); i++)
    y[i] = resultString[i];
    ptr(y);
    }

    now this code works fine to the point where i get data i.e. this function "Callback1(stri ng xmlString)"

    but as soon as debugger reaches at the end of this function program crashes
    I have tried to run release version it crashes too and when i try to debug this program visual studio tells me this

    "A buffer overrun has occurred in demotest.exe which has corrupted the program's internal state. Press Break to debug the program or Continue to terminate the program."

    Now i have tried a null terminated string too but that doesnt seem to help

    So i need help of expert people here
    been 2 weeks and i have not been able to solve this problem even after looking at all the example on internet.
  • zahid313
    New Member
    • Nov 2008
    • 20

    #2
    my c++ project which makes the dll is unmanaged code

    Comment

    • zahid313
      New Member
      • Nov 2008
      • 20

      #3
      I am still trying and waiting for your help

      Comment

      • zahid313
        New Member
        • Nov 2008
        • 20

        #4
        I have tried sending "int" from c++ to C# and i got no error
        so i am convinced this has some thing to do with data marshaling

        Comment

        • zahid313
          New Member
          • Nov 2008
          • 20

          #5
          infact i just found out that my code doesnt give any error as long as i dont declare std::string in my function...mean s it also works fine with char *.
          which means i have to stop using std::string or there is still some other way around

          Comment

          • zahid313
            New Member
            • Nov 2008
            • 20

            #6
            now this is the exception that is being thrown at C# end

            "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

            I think the pointer gets corrupted any idea how can solve it

            my dll which is written in unmanaged c++
            has a xmlrpc server that listens for any requests as soon as a request comes in it executes that method and does callback to c# client

            I really need help on this

            Comment

            • zahid313
              New Member
              • Nov 2008
              • 20

              #7
              my c# client calls this function

              void GetScreenConten tsCallBack(void *functocall)
              {
              ptr = (Myfunctocall)f unctocall;
              // Create the server socket on the specified port
              s.bindAndListen (50055);
              // Enable introspection
              s.enableIntrosp ection(true);
              // Wait for requests indefinitely
              s.work(-1.0);
              }

              where "s" is object of XMLRPCServer
              and ptr is defined as
              typedef void (*Myfunctocall) (const char *xmlString);
              Myfunctocall ptr;

              this class has a function that gets called
              class GetScreenConten ts : public XmlRpcServerMet hod
              {
              public:
              GetScreenConten ts(XmlRpcServer * s) : XmlRpcServerMet hod("ITC.GetScr eenContents", s) {}

              void execute(XmlRpcV alue& params, XmlRpcValue& result)
              {
              std::string resultString ="ABC";
              y = new char[resultString.le ngth()];
              for(int i = 0; i < resultString.le ngth(); i++)
              {
              y[i] = resultString[i];
              }
              std::strcat(y," \0");
              ptr(y);
              }
              } getScreenConten ts(&s);

              I hope this is the right place to ask i mean in .net forum
              because exception is being thrown at C# end

              Comment

              Working...