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. Type convertion and Unmanaged code

Type convertion and Unmanaged code

Scheduled Pinned Locked Moved C#
csharphelp
6 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
    Shahin77
    wrote on last edited by
    #1

    :doh:Hi guys, Im trying to convert some visual basic code into c#, dose anybody know what's the equal things in c# for following function declaration and type. any help is appreciated. Public Declare Function Test Lib "x.dll" _ Alias "#3" (ByVal F1 As String, ByVal F2 As Long, ByVal F3 As Long, CDCAr As CDCAResult) As Long Type CDCAResult ID_0 As Byte ID_1 As String * 17 ID_2(0 To 11) As Long ID_3(0 To 7) As String * 41 End Type

    H 1 Reply Last reply
    0
    • S Shahin77

      :doh:Hi guys, Im trying to convert some visual basic code into c#, dose anybody know what's the equal things in c# for following function declaration and type. any help is appreciated. Public Declare Function Test Lib "x.dll" _ Alias "#3" (ByVal F1 As String, ByVal F2 As Long, ByVal F3 As Long, CDCAr As CDCAResult) As Long Type CDCAResult ID_0 As Byte ID_1 As String * 17 ID_2(0 To 11) As Long ID_3(0 To 7) As String * 41 End Type

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      For information on P/Invoking native functions, see the DllImportAttribute documentation in the .NET Framework SDK. An example follows:

      [DllImport("x.dll")]
      private static extern long Test(
      string f1, long f2, long f3, CDCAResult CDCAr);

      For the struct, you create the managed version just like you would re-create it in VB:

      [StructLayout(LayoutKind.Sequential)]
      public struct CDCAResult
      {
      public byte id0;
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst=17)]public string id1;
      public long id2;
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst=41)]public string id3;
      }

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      S 1 Reply Last reply
      0
      • H Heath Stewart

        For information on P/Invoking native functions, see the DllImportAttribute documentation in the .NET Framework SDK. An example follows:

        [DllImport("x.dll")]
        private static extern long Test(
        string f1, long f2, long f3, CDCAResult CDCAr);

        For the struct, you create the managed version just like you would re-create it in VB:

        [StructLayout(LayoutKind.Sequential)]
        public struct CDCAResult
        {
        public byte id0;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst=17)]public string id1;
        public long id2;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst=41)]public string id3;
        }

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

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

        thanx for reply, I have 2 question: 1- Is it the same declaration for Fixed-Length String and arrays which contains Fixed-Length String for exapmle: F1 As String * 31 F2(0 To 7) As String * 41 2- when i use like this [DllImport("x.dll")]private static extern long Test( string f1, long f2, long f3, CDCAResult CDCAr); i get error, but if i use by reference like this: [DllImport("x.dll")]private static extern long Test( string f1, long f2, long f3, [MarshalAs(UnmanagedType.Struct)] ref CDCAResult CDCAr); it works without error, but still dosen't return correct value for Fixed-Length String in that structure. any help really appreciated. best regards

        H 2 Replies Last reply
        0
        • S Shahin77

          thanx for reply, I have 2 question: 1- Is it the same declaration for Fixed-Length String and arrays which contains Fixed-Length String for exapmle: F1 As String * 31 F2(0 To 7) As String * 41 2- when i use like this [DllImport("x.dll")]private static extern long Test( string f1, long f2, long f3, CDCAResult CDCAr); i get error, but if i use by reference like this: [DllImport("x.dll")]private static extern long Test( string f1, long f2, long f3, [MarshalAs(UnmanagedType.Struct)] ref CDCAResult CDCAr); it works without error, but still dosen't return correct value for Fixed-Length String in that structure. any help really appreciated. best regards

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          Sorry, didn't notice that the last param in your Function was a ByRef. Yes, the ref keyword would be necessary then. You could also use the out keyword if you don't need to pass an initialized struct into the function. That would save you from having to instantiate the struct before calling the method. As far as the fixed-length string question, you'll have to jog my memory on the F2(0 To 7) As String * 41 syntax. It's been a LONG time since I programmed in VB.

          -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

          1 Reply Last reply
          0
          • S Shahin77

            thanx for reply, I have 2 question: 1- Is it the same declaration for Fixed-Length String and arrays which contains Fixed-Length String for exapmle: F1 As String * 31 F2(0 To 7) As String * 41 2- when i use like this [DllImport("x.dll")]private static extern long Test( string f1, long f2, long f3, CDCAResult CDCAr); i get error, but if i use by reference like this: [DllImport("x.dll")]private static extern long Test( string f1, long f2, long f3, [MarshalAs(UnmanagedType.Struct)] ref CDCAResult CDCAr); it works without error, but still dosen't return correct value for Fixed-Length String in that structure. any help really appreciated. best regards

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            I also got to thinking that the encoding could be the problem. Does this function that you're wanting to P/Invoke only support ANSI characters, Unicode characters, or either ANSI or Unicode depending on the operating system? Depending on what encoding it uses, you should add the CharSet property in the StructLayoutAttribute:

            [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
            public struct CDCAResult
            {
            // ...
            }

            The SizeConst property in the MarshalAsAttribute will make sure that the offsets are correct for the String fields, so assuming that the rest of your fields where the values you were expecting the character encoding is most likely the problem.

            -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

            S 1 Reply Last reply
            0
            • H Heath Stewart

              I also got to thinking that the encoding could be the problem. Does this function that you're wanting to P/Invoke only support ANSI characters, Unicode characters, or either ANSI or Unicode depending on the operating system? Depending on what encoding it uses, you should add the CharSet property in the StructLayoutAttribute:

              [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
              public struct CDCAResult
              {
              // ...
              }

              The SizeConst property in the MarshalAsAttribute will make sure that the offsets are correct for the String fields, so assuming that the rest of your fields where the values you were expecting the character encoding is most likely the problem.

              -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

              S Offline
              S Offline
              Shahin77
              wrote on last edited by
              #6

              you are right, encoding was problem, that unmanaged dll dose not support Unicode, when i use Ansi, every things gose through. I really appriciate your help and knowledge.:suss:

              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