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. How to transfer a custom struct pointer to DLL's function?

How to transfer a custom struct pointer to DLL's function?

Scheduled Pinned Locked Moved C#
questioncsharpc++data-structureshelp
3 Posts 2 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
    samfromcn
    wrote on last edited by
    #1

    I has a C++ DLL, it has follow code: typedef struct _SC_CONTEXT{ long hPort; long dwCardType; // BYTE params[24]; } SC_CONTEXT; extern int __stdcall TestFunc(SC_CONTEXT* p) { char buf[256]; sprintf(buf, "hPort is : %d, dwCardType is : %d.", p.hPort, p.dwCardType); // sprintf(buf, "hPort is : %d, dwCardType is : %d, params[0] is : %d.", p->hPort, p->dwCardType, p->params[0]); MessageBoxA(NULL, buf, "caption1", MB_OK); return 0; } I wrote C# code like : [StructLayout(LayoutKind.Sequential)] public struct _SC_CONTEXT { public long hPort; public long dwCardType; } namespace MyName { class MyClass { [DllImport("testDll.dll")] public static extern int TestFunc(ref _SC_CONTEXT11 a); } static void Main(string[] args) { unsafe { _SC_CONTEXT a;// = new _SC_CONTEXT(); a.hPort = 67; a.dwCardType = 66; // a.paras[0] = 255; TestFunc(ref a); } ...... } } the question is, function TestFunc can not get _SC_CONTEXT::dwCardType's value. In the messagebox, content always is "hPort is : 67, dwCardType is : 0.". I want to know why, and how to correct this bug. As you see, I comment an array member of the struct, if not comment it, how to use struct with array member.:confused:

    M 1 Reply Last reply
    0
    • S samfromcn

      I has a C++ DLL, it has follow code: typedef struct _SC_CONTEXT{ long hPort; long dwCardType; // BYTE params[24]; } SC_CONTEXT; extern int __stdcall TestFunc(SC_CONTEXT* p) { char buf[256]; sprintf(buf, "hPort is : %d, dwCardType is : %d.", p.hPort, p.dwCardType); // sprintf(buf, "hPort is : %d, dwCardType is : %d, params[0] is : %d.", p->hPort, p->dwCardType, p->params[0]); MessageBoxA(NULL, buf, "caption1", MB_OK); return 0; } I wrote C# code like : [StructLayout(LayoutKind.Sequential)] public struct _SC_CONTEXT { public long hPort; public long dwCardType; } namespace MyName { class MyClass { [DllImport("testDll.dll")] public static extern int TestFunc(ref _SC_CONTEXT11 a); } static void Main(string[] args) { unsafe { _SC_CONTEXT a;// = new _SC_CONTEXT(); a.hPort = 67; a.dwCardType = 66; // a.paras[0] = 255; TestFunc(ref a); } ...... } } the question is, function TestFunc can not get _SC_CONTEXT::dwCardType's value. In the messagebox, content always is "hPort is : 67, dwCardType is : 0.". I want to know why, and how to correct this bug. As you see, I comment an array member of the struct, if not comment it, how to use struct with array member.:confused:

      M Offline
      M Offline
      mav northwind
      wrote on last edited by
      #2

      I've spotted at least one bug immediately: .NET long is 64bit, but in C++ it's 32bit! So you should declare your struct with int instead:

      samfromcn wrote:

      public struct _SC_CONTEXT { public long hPort; public long dwCardType; }

      public struct _SC_CONTEXT
      {
      public int hPort;
      public int dwCardType;
      }

      Regards, mav -- Black holes are the places where God divided by 0...

      S 1 Reply Last reply
      0
      • M mav northwind

        I've spotted at least one bug immediately: .NET long is 64bit, but in C++ it's 32bit! So you should declare your struct with int instead:

        samfromcn wrote:

        public struct _SC_CONTEXT { public long hPort; public long dwCardType; }

        public struct _SC_CONTEXT
        {
        public int hPort;
        public int dwCardType;
        }

        Regards, mav -- Black holes are the places where God divided by 0...

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

        Thank you very much. All questions are resolved. :laugh: :rose:

        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