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
  1. Home
  2. General Programming
  3. C#
  4. 8-O fixing C# class in unsafe code block???

8-O fixing C# class in unsafe code block???

Scheduled Pinned Locked Moved C#
csharpc++helpquestion
8 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.
  • C Offline
    C Offline
    Chesnokov Yuriy
    wrote on last edited by
    #1

    I've got some public class class Foo { public int a; public int b; ... } And I'd like to pass its instance to some unmanaged lib function, which has the same struct with int a, int b, etc... C# Foo foo = new Foo(); fixed (void* pfoo = foo) //<--------- error???? { someFunction(pfoo); } [DllImport("somelib")] static extern unsafe int someFunction(void* p); C++ typedef struct _somestr { int a; int b; int c; ... }SOMESTR; __declspec(dllexport) someFunction(SOMESRT* p) { p->a = 10; p->b = p->c; }

    chesnokov

    L N 2 Replies Last reply
    0
    • C Chesnokov Yuriy

      I've got some public class class Foo { public int a; public int b; ... } And I'd like to pass its instance to some unmanaged lib function, which has the same struct with int a, int b, etc... C# Foo foo = new Foo(); fixed (void* pfoo = foo) //<--------- error???? { someFunction(pfoo); } [DllImport("somelib")] static extern unsafe int someFunction(void* p); C++ typedef struct _somestr { int a; int b; int c; ... }SOMESTR; __declspec(dllexport) someFunction(SOMESRT* p) { p->a = 10; p->b = p->c; }

      chesnokov

      L Offline
      L Offline
      lisan_al_ghaib
      wrote on last edited by
      #2

      Hi declare your function like this : DllImport("somelib")] static extern unsafe int someFunction(IntPtr p); then use marshalling to reconstruct your C++ strucutre -> into a C# 'struct' (google it) In your DLL Use CoTaskMemAlloc function to allocate memory To free use CoTaskMemFree

      C 1 Reply Last reply
      0
      • C Chesnokov Yuriy

        I've got some public class class Foo { public int a; public int b; ... } And I'd like to pass its instance to some unmanaged lib function, which has the same struct with int a, int b, etc... C# Foo foo = new Foo(); fixed (void* pfoo = foo) //<--------- error???? { someFunction(pfoo); } [DllImport("somelib")] static extern unsafe int someFunction(void* p); C++ typedef struct _somestr { int a; int b; int c; ... }SOMESTR; __declspec(dllexport) someFunction(SOMESRT* p) { p->a = 10; p->b = p->c; }

        chesnokov

        N Offline
        N Offline
        Nicholas Butler
        wrote on last edited by
        #3

        As lisan said, you have to use a struct. You probably also want this attribute:

        [ StructLayout( LayoutKind.Sequential ) ]
        struct ...

        Then you have to take the address of the struct:

        fixed( void* pfoo = & foo )

        You can have a look at http://pinvoke.net/[^] for some examples. Nick

        ---------------------------------- Be excellent to each other :)

        C 1 Reply Last reply
        0
        • L lisan_al_ghaib

          Hi declare your function like this : DllImport("somelib")] static extern unsafe int someFunction(IntPtr p); then use marshalling to reconstruct your C++ strucutre -> into a C# 'struct' (google it) In your DLL Use CoTaskMemAlloc function to allocate memory To free use CoTaskMemFree

          C Offline
          C Offline
          Chesnokov Yuriy
          wrote on last edited by
          #4

          ?? there is a C# structure, already allocated. I need to pass it to C++ function in the dll. Please the code snippet.

          chesnokov

          1 Reply Last reply
          0
          • N Nicholas Butler

            As lisan said, you have to use a struct. You probably also want this attribute:

            [ StructLayout( LayoutKind.Sequential ) ]
            struct ...

            Then you have to take the address of the struct:

            fixed( void* pfoo = & foo )

            You can have a look at http://pinvoke.net/[^] for some examples. Nick

            ---------------------------------- Be excellent to each other :)

            C Offline
            C Offline
            Chesnokov Yuriy
            wrote on last edited by
            #5

            fixed( void* pfoo = & foo ) Error 1 You cannot use the fixed statement to take the address of an already fixed expression fixed (void* pp = p) Error 1 Cannot implicitly convert type 'SomeStruct' to 'void*'

            chesnokov

            G L 2 Replies Last reply
            0
            • C Chesnokov Yuriy

              fixed( void* pfoo = & foo ) Error 1 You cannot use the fixed statement to take the address of an already fixed expression fixed (void* pp = p) Error 1 Cannot implicitly convert type 'SomeStruct' to 'void*'

              chesnokov

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              Chesnokov Yuriy wrote:

              fixed( void* pfoo = & foo ) Error 1 You cannot use the fixed statement to take the address of an already fixed expression

              That's because foo is a local variable, so it's already in a fixed position on the stack. You can only fix objects that is in the heap. Just get the address of the struct: void* pfoo = &foo;

              Despite everything, the person most likely to be fooling you next is yourself.

              C 1 Reply Last reply
              0
              • C Chesnokov Yuriy

                fixed( void* pfoo = & foo ) Error 1 You cannot use the fixed statement to take the address of an already fixed expression fixed (void* pp = p) Error 1 Cannot implicitly convert type 'SomeStruct' to 'void*'

                chesnokov

                L Offline
                L Offline
                lisan_al_ghaib
                wrote on last edited by
                #7

                You have to use IntPtr instead of void use Marshall.CoTaskMemAlloc method to allocate Marshall.CoTaskMemFree method to free after using your struct

                1 Reply Last reply
                0
                • G Guffa

                  Chesnokov Yuriy wrote:

                  fixed( void* pfoo = & foo ) Error 1 You cannot use the fixed statement to take the address of an already fixed expression

                  That's because foo is a local variable, so it's already in a fixed position on the stack. You can only fix objects that is in the heap. Just get the address of the struct: void* pfoo = &foo;

                  Despite everything, the person most likely to be fooling you next is yourself.

                  C Offline
                  C Offline
                  Chesnokov Yuriy
                  wrote on last edited by
                  #8

                  bingo! Put the struct as a class field and allocate it there. Can I allocate it in a function that it goes onto heap??

                  chesnokov

                  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