Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Marshalling problem (from managed to unmanaged)

Marshalling problem (from managed to unmanaged)

Scheduled Pinned Locked Moved Managed C++/CLI
c++helpperformancetutorial
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    s196675m
    wrote on last edited by
    #1

    HOPEFULLY someone can help me. I am trying to access a function from an unmanaged DLL from my visual c++ managed GUI. Declaration of the function I am trying to import from unmanaged (native) DLL is: extern "C" __declspec(dllexport) TGrayImage* init_seg(TGrayImage* pImg, TMonoImage* monoImg, int nClass) Even though there are many functons in the DLL, only this function is exported. Now In my GUI, I have added the following line to import that functon so that I can call that functon from GUI. At the top of the file after using namespaces [System::Runtime::InteropServices::DllImport("segment.dll")] TGrayImage * init_seg(TGrayImage* pimg, TMonoImage* mono, int nNum); I didn't do any Marshalling parameter. I have no idea how to Marshall a pointer to a Template class or pointer to a class. I searched before posting but I didn't get clear understanding about it. Just for clarification, pimg and mono I am trying to pass is Managed varialble created within managed GUI. TGrayImage* pimg = new TGrayImage* (width,height);// used new not gcnew TmonoImage* mono = new TMonoImage(width, height); I have added the proper library (compiled with pure:MSIL) in my my GUI which supplied TGrayImage,TMonoImage. Header file for TGrayImage and TMonoImage already included in the GUI. When I call init_seg function from GUI, I got the following message: An unhandled exception of type 'System.AccessViolationException' occurred in GUI.exe Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Please help me to Marshall these parameter correctway. I will appreciate your big help. Thank you.

    L M 2 Replies Last reply
    0
    • S s196675m

      HOPEFULLY someone can help me. I am trying to access a function from an unmanaged DLL from my visual c++ managed GUI. Declaration of the function I am trying to import from unmanaged (native) DLL is: extern "C" __declspec(dllexport) TGrayImage* init_seg(TGrayImage* pImg, TMonoImage* monoImg, int nClass) Even though there are many functons in the DLL, only this function is exported. Now In my GUI, I have added the following line to import that functon so that I can call that functon from GUI. At the top of the file after using namespaces [System::Runtime::InteropServices::DllImport("segment.dll")] TGrayImage * init_seg(TGrayImage* pimg, TMonoImage* mono, int nNum); I didn't do any Marshalling parameter. I have no idea how to Marshall a pointer to a Template class or pointer to a class. I searched before posting but I didn't get clear understanding about it. Just for clarification, pimg and mono I am trying to pass is Managed varialble created within managed GUI. TGrayImage* pimg = new TGrayImage* (width,height);// used new not gcnew TmonoImage* mono = new TMonoImage(width, height); I have added the proper library (compiled with pure:MSIL) in my my GUI which supplied TGrayImage,TMonoImage. Header file for TGrayImage and TMonoImage already included in the GUI. When I call init_seg function from GUI, I got the following message: An unhandled exception of type 'System.AccessViolationException' occurred in GUI.exe Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Please help me to Marshall these parameter correctway. I will appreciate your big help. Thank you.

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      I can't decipher your post.

      s196675m wrote:

      I am trying to access a function from an unmanaged DLL from my visual c++ managed GUI. Declaration of the function I am trying to import from unmanaged (native) DLL is: extern "C" __declspec(dllexport) TGrayImage* init_seg(TGrayImage* pImg, TMonoImage* monoImg, int nClass)

      First you indicate an unmanged source, then:

      s196675m wrote:

      Just for clarification, pimg and mono I am trying to pass is Managed varialble created within managed GUI. TGrayImage* pimg = new TGrayImage* (width,height);// used new not gcnew TmonoImage* mono = new TMonoImage(width, height); I have added the proper library (compiled with pure:MSIL) in my my GUI which supplied TGrayImage,TMonoImage.

      At this point I am at a complete loss to understand your scenario.

      led mike

      S 1 Reply Last reply
      0
      • L led mike

        I can't decipher your post.

        s196675m wrote:

        I am trying to access a function from an unmanaged DLL from my visual c++ managed GUI. Declaration of the function I am trying to import from unmanaged (native) DLL is: extern "C" __declspec(dllexport) TGrayImage* init_seg(TGrayImage* pImg, TMonoImage* monoImg, int nClass)

        First you indicate an unmanged source, then:

        s196675m wrote:

        Just for clarification, pimg and mono I am trying to pass is Managed varialble created within managed GUI. TGrayImage* pimg = new TGrayImage* (width,height);// used new not gcnew TmonoImage* mono = new TMonoImage(width, height); I have added the proper library (compiled with pure:MSIL) in my my GUI which supplied TGrayImage,TMonoImage.

        At this point I am at a complete loss to understand your scenario.

        led mike

        S Offline
        S Offline
        s196675m
        wrote on last edited by
        #3

        Sorry, for ambiguity.

        led mike wrote:

        s196675m wrote: I am trying to access a function from an unmanaged DLL from my visual c++ managed GUI. Declaration of the function I am trying to import from unmanaged (native) DLL is: extern "C" __declspec(dllexport) TGrayImage* init_seg(TGrayImage* pImg, TMonoImage* monoImg, int nClass) First you indicate an unmanged source, then:

        Yes you are right. This line of declaration from unmanged source. (part of unmanaged DLL.

        led mike wrote:

        s196675m wrote: Just for clarification, pimg and mono I am trying to pass is Managed varialble created within managed GUI. TGrayImage* pimg = new TGrayImage* (width,height);// used new not gcnew TmonoImage* mono = new TMonoImage(width, height); These sample code is part of GUI( managed). I just showed here how I initialize pimg and mono variables before passing to the imported function I have added the proper library (compiled with pure:MSIL) in my my GUI which supplied TGrayImage,TMonoImage. Here I am trying to mean, The other library I used with GUI also complied as a managed library. At this point I am at a complete loss to understand your scenario.

        Now my main question is: (1) Did I imported the function correctly ? (2) Do I need to marshall the parameters before passing to the function? (3) How Can I marshall the first two parameters? Thank you.

        L 1 Reply Last reply
        0
        • S s196675m

          HOPEFULLY someone can help me. I am trying to access a function from an unmanaged DLL from my visual c++ managed GUI. Declaration of the function I am trying to import from unmanaged (native) DLL is: extern "C" __declspec(dllexport) TGrayImage* init_seg(TGrayImage* pImg, TMonoImage* monoImg, int nClass) Even though there are many functons in the DLL, only this function is exported. Now In my GUI, I have added the following line to import that functon so that I can call that functon from GUI. At the top of the file after using namespaces [System::Runtime::InteropServices::DllImport("segment.dll")] TGrayImage * init_seg(TGrayImage* pimg, TMonoImage* mono, int nNum); I didn't do any Marshalling parameter. I have no idea how to Marshall a pointer to a Template class or pointer to a class. I searched before posting but I didn't get clear understanding about it. Just for clarification, pimg and mono I am trying to pass is Managed varialble created within managed GUI. TGrayImage* pimg = new TGrayImage* (width,height);// used new not gcnew TmonoImage* mono = new TMonoImage(width, height); I have added the proper library (compiled with pure:MSIL) in my my GUI which supplied TGrayImage,TMonoImage. Header file for TGrayImage and TMonoImage already included in the GUI. When I call init_seg function from GUI, I got the following message: An unhandled exception of type 'System.AccessViolationException' occurred in GUI.exe Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Please help me to Marshall these parameter correctway. I will appreciate your big help. Thank you.

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          The passed pointers should marshal no problem. Where exactly is the exception occurring?  Can you step into the init_seg() call in the debugger? Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          U 1 Reply Last reply
          0
          • S s196675m

            Sorry, for ambiguity.

            led mike wrote:

            s196675m wrote: I am trying to access a function from an unmanaged DLL from my visual c++ managed GUI. Declaration of the function I am trying to import from unmanaged (native) DLL is: extern "C" __declspec(dllexport) TGrayImage* init_seg(TGrayImage* pImg, TMonoImage* monoImg, int nClass) First you indicate an unmanged source, then:

            Yes you are right. This line of declaration from unmanged source. (part of unmanaged DLL.

            led mike wrote:

            s196675m wrote: Just for clarification, pimg and mono I am trying to pass is Managed varialble created within managed GUI. TGrayImage* pimg = new TGrayImage* (width,height);// used new not gcnew TmonoImage* mono = new TMonoImage(width, height); These sample code is part of GUI( managed). I just showed here how I initialize pimg and mono variables before passing to the imported function I have added the proper library (compiled with pure:MSIL) in my my GUI which supplied TGrayImage,TMonoImage. Here I am trying to mean, The other library I used with GUI also complied as a managed library. At this point I am at a complete loss to understand your scenario.

            Now my main question is: (1) Did I imported the function correctly ? (2) Do I need to marshall the parameters before passing to the function? (3) How Can I marshall the first two parameters? Thank you.

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #5

            s196675m wrote:

            Now my main question is:

            Sorry but have not at all cleared up the seeming contradictory statements in your orginal post

            s196675m wrote:

            I have added the proper library (compiled with pure:MSIL) in my my GUI which supplied TGrayImage,TMonoImage. Here I am trying to mean, The other library I used with GUI also complied as a managed library.

            First you say it's a native library then you seem to be saying you compiled it as a managed library (pure:MSIL). It can't be both so I still have no idea what you are saying.

            led mike

            1 Reply Last reply
            0
            • M Mark Salsbery

              The passed pointers should marshal no problem. Where exactly is the exception occurring?  Can you step into the init_seg() call in the debugger? Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              U Offline
              U Offline
              uusheikh
              wrote on last edited by
              #6

              I think the easiest way is to use #pragma unmanaged/managed. Set your project properties to /clr, create a native function and wrap it with #pragma unmanaged/managed, include your .h native library header and link to your .lib. Call the native function from your GUI.

              M 1 Reply Last reply
              0
              • U uusheikh

                I think the easiest way is to use #pragma unmanaged/managed. Set your project properties to /clr, create a native function and wrap it with #pragma unmanaged/managed, include your .h native library header and link to your .lib. Call the native function from your GUI.

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                Maybe, but I tried code like the OP's and it works fine.  Until (s)he answers with better error info, it's hard to tell, but I think the error has nothing to do with marshaling. Mark

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                L 1 Reply Last reply
                0
                • M Mark Salsbery

                  Maybe, but I tried code like the OP's and it works fine.  Until (s)he answers with better error info, it's hard to tell, but I think the error has nothing to do with marshaling. Mark

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  L Offline
                  L Offline
                  led mike
                  wrote on last edited by
                  #8

                  Mark Salsbery wrote:

                  but I think the error has nothing to do with marshaling.

                  It's far more likely it's due to copy/paste coding rather than actually understanding the technologies you are attempting to utilize. By the way, what the heck were you doing being on here 7 hours ago? Get some sleep ;P

                  led mike

                  M 1 Reply Last reply
                  0
                  • L led mike

                    Mark Salsbery wrote:

                    but I think the error has nothing to do with marshaling.

                    It's far more likely it's due to copy/paste coding rather than actually understanding the technologies you are attempting to utilize. By the way, what the heck were you doing being on here 7 hours ago? Get some sleep ;P

                    led mike

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #9

                    Agreed.

                    led mike wrote:

                    By the way, what the heck were you doing being on here 7 hours ago?

                    Hehe it's hectic around here.  My GF is working long hours here on graduation projects for her second degree. So I just hang out getting some work done or playing on CP...until the screenwriters start cranking out new TV show episodes again ;P  I work at home so regular work hours just don't exist.

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups