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. C# and old "C" Dlls

C# and old "C" Dlls

Scheduled Pinned Locked Moved C#
csharpdata-structureshelpquestionlearning
2 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.
  • M Offline
    M Offline
    Mike Doner
    wrote on last edited by
    #1

    Im in the process of learning C#, and so far, I'm loving it. I've found a way to make calls to some old "C" DLLs, but I have a few questions regarding types. [DllImport("MYDLL.DLL")] public static extern ulong GetSerialNumbers ( byte f1, byte f2, byte NodeAddr, PIFS_CABLESER pserials, PUSHORT pcount ); I have a function in in MYDLL.DLL called GetSerialNumbers that returns an array of structures. I have no problems declaring functions with known types, but as soon as I add in a type that C# doesn't know, I'm stuck. I would like to take the data of these structures and copy them to a class I've created.... This is my C definition, would I have to make something equivalent that C# will understand? typedef struct SerialInfoMD { byte ID[8]; byte something; int new; } IFS_CABLESER, *PIFS_CABLE_SER; Anybody have any info to share? Thanks! Mike

    L 1 Reply Last reply
    0
    • M Mike Doner

      Im in the process of learning C#, and so far, I'm loving it. I've found a way to make calls to some old "C" DLLs, but I have a few questions regarding types. [DllImport("MYDLL.DLL")] public static extern ulong GetSerialNumbers ( byte f1, byte f2, byte NodeAddr, PIFS_CABLESER pserials, PUSHORT pcount ); I have a function in in MYDLL.DLL called GetSerialNumbers that returns an array of structures. I have no problems declaring functions with known types, but as soon as I add in a type that C# doesn't know, I'm stuck. I would like to take the data of these structures and copy them to a class I've created.... This is my C definition, would I have to make something equivalent that C# will understand? typedef struct SerialInfoMD { byte ID[8]; byte something; int new; } IFS_CABLESER, *PIFS_CABLE_SER; Anybody have any info to share? Thanks! Mike

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

      Yes, as with the requirement of defining the function signature in C# you also have to supply a description of the structure as well. For the structure you provided the sig would be [StructLayout(LayoutKind.Sequential)] struct SerialInfoMD { [MarshalAs(UnmanagedType.ByValArray, SizeConst=8)] public byte[] ID; public byte Something; public int New; // note the capitalization change. Syntax error otherwise. } Things can get tricky when marshalling data between managed and unmanaged code. For example, in your function signature you have the return type as ulong. Is the return type for the C function an unsigned long? Unsigned long is 32-bits on Win32 but is 64-bits on .NET so the appropriate return type in the C# method is uint if the return type on the C function is unsigned long. It gets even more murky when dealing with arrays. You might wanna check out information on data marshalling on MSDN. Try this URL for a start. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconInteropMarshaling.asp

      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