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. C#
  4. Unmanaged C#, c++ compiled dll

Unmanaged C#, c++ compiled dll

Scheduled Pinned Locked Moved C#
questioncsharpc++helptutorial
5 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.
  • M Offline
    M Offline
    magnat69
    wrote on last edited by
    #1

    Hi, i have a dynamic link library (compiled in VC++ 7.0) and want to use the functions(and types,structs) in my c# program, i already read some articles on this msg-board about unmanaged code, just those only show easy functions with simple return-paramaters. eg.: [DllImport("kernel32")] public static extern void Sleep(int dwMilliseconds); What if the functions return parameter is a pointer to a complex structure(eg.: chained lists)? As example, i am trying to import functions from a packet construction library called libnet, alone the init function of this library looks like this: libnet_t * libnet_init(int injection_type, int8_t *device, int8_t *err_buf); My question now is can someone explain/show/link me how i marshall such return/call by reference variables, will i have to write a wrapper class which acts as a interface between my c# program and the c++ dll, if yes how? If you can help provide an easy code snippet and show me how i need to start or point me to a book/website/etc. Thanks in advance....

    C J S 3 Replies Last reply
    0
    • M magnat69

      Hi, i have a dynamic link library (compiled in VC++ 7.0) and want to use the functions(and types,structs) in my c# program, i already read some articles on this msg-board about unmanaged code, just those only show easy functions with simple return-paramaters. eg.: [DllImport("kernel32")] public static extern void Sleep(int dwMilliseconds); What if the functions return parameter is a pointer to a complex structure(eg.: chained lists)? As example, i am trying to import functions from a packet construction library called libnet, alone the init function of this library looks like this: libnet_t * libnet_init(int injection_type, int8_t *device, int8_t *err_buf); My question now is can someone explain/show/link me how i marshall such return/call by reference variables, will i have to write a wrapper class which acts as a interface between my c# program and the c++ dll, if yes how? If you can help provide an easy code snippet and show me how i need to start or point me to a book/website/etc. Thanks in advance....

      C Offline
      C Offline
      Corinna John
      wrote on last edited by
      #2

      Declare the function with return type "IntPtr". An IntPtr can point to any address in memory, and you can de-marshal the object: IntPtr ptr = libnet_init(...); Object obj = Marshal.PtrToStructure(ptr); to use the object in C#, you have to write an interface.

      M 1 Reply Last reply
      0
      • M magnat69

        Hi, i have a dynamic link library (compiled in VC++ 7.0) and want to use the functions(and types,structs) in my c# program, i already read some articles on this msg-board about unmanaged code, just those only show easy functions with simple return-paramaters. eg.: [DllImport("kernel32")] public static extern void Sleep(int dwMilliseconds); What if the functions return parameter is a pointer to a complex structure(eg.: chained lists)? As example, i am trying to import functions from a packet construction library called libnet, alone the init function of this library looks like this: libnet_t * libnet_init(int injection_type, int8_t *device, int8_t *err_buf); My question now is can someone explain/show/link me how i marshall such return/call by reference variables, will i have to write a wrapper class which acts as a interface between my c# program and the c++ dll, if yes how? If you can help provide an easy code snippet and show me how i need to start or point me to a book/website/etc. Thanks in advance....

        J Offline
        J Offline
        Jeremy Kimball
        wrote on last edited by
        #3

        There are a few ways of doing this: 1. Hardest solution: Write a custom marshaller in Managed C++ that converts(or wraps) the non-managed type into a managed type; 2. Take the approach of returning an IntPtr, then defining a custom interface to the embedded type. 3. Easiest solution: If the return type can be decomposed into simpler managed variable types, define the structure in the managed part: function decl:

        my_type* getMyTypeStuff( .... );

        where my_type =

        struct my_type {
        int foo;
        int bar;
        HWND* gobble;
        char[] yadda;
        }...;

        managed declarations:

        struct MyType {
        Int32 foo;
        Int32 bar;
        IntPtr gobble;
        byte[] yadda (or char[] yadda, or IntPtr yadda...)
        }

        [DllImport(....)]
        public static extern MyType[] getMyTypeStuff(....);

        You may need a MarshalAs attribute in there, not sure, I'm running from memory. Actually, take all of the above with a grain of salt, as I don't have any reference material in front of me at the moment... Jeremy Kimball

        1 Reply Last reply
        0
        • C Corinna John

          Declare the function with return type "IntPtr". An IntPtr can point to any address in memory, and you can de-marshal the object: IntPtr ptr = libnet_init(...); Object obj = Marshal.PtrToStructure(ptr); to use the object in C#, you have to write an interface.

          M Offline
          M Offline
          magnat69
          wrote on last edited by
          #4

          Hope you can give me a little bit more help anyway here is code snippet how i would use the functions / structs in c++ and create a simple TCP packet. #include int main(int argc, char **argv) { libnet_t *lib_pointer = NULL; //type from libnet.h libnet_ptag_t tcp,ip; //type from libnet.h char *device = NULL; char errbuff[1024]; write int; lib_pointer = libnet_init(LIBNET_RAW4, device, errbuff); //build TCP tcp = libnet_build_tcp(..,..,..,.,lib_pointer,...); //build IP ip = libnet_build_ip(..,..,..,lib_pointer,.....); //lets put the packet on the wire write=libnet_write(lib_pointer); My main question is how can i use the types, consts which are defined in libnet.h ala the .dll file i compiled in vc++. You made the statement about this IntPtr, which i marshal to a PtrToStruct, well so i could also create a PtrToStruct type in c# and let it point to eg. libnet_t? What i did not really understand is the interface thing you mentioned, could you explain this a bit more deeply? Thanks for your time again.

          1 Reply Last reply
          0
          • M magnat69

            Hi, i have a dynamic link library (compiled in VC++ 7.0) and want to use the functions(and types,structs) in my c# program, i already read some articles on this msg-board about unmanaged code, just those only show easy functions with simple return-paramaters. eg.: [DllImport("kernel32")] public static extern void Sleep(int dwMilliseconds); What if the functions return parameter is a pointer to a complex structure(eg.: chained lists)? As example, i am trying to import functions from a packet construction library called libnet, alone the init function of this library looks like this: libnet_t * libnet_init(int injection_type, int8_t *device, int8_t *err_buf); My question now is can someone explain/show/link me how i marshall such return/call by reference variables, will i have to write a wrapper class which acts as a interface between my c# program and the c++ dll, if yes how? If you can help provide an easy code snippet and show me how i need to start or point me to a book/website/etc. Thanks in advance....

            S Offline
            S Offline
            Sphynx_Roland
            wrote on last edited by
            #5

            You could also make use of unsafe coding by using the unsafe keyword. Make shure to set up the compiler options for allowing unsafe code in the project. For further instructions check the help provided with your c# package.

            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