Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

Converting VB6 to C#

Hi Experts,

I have a VB6 program that I am currently converting to C# and I am having issues interfacing to a particular method in an existing unmanaged dll.

All calls to the dll so far have worked perfectly, but this is the only method that passes a reference to a struct and is the only one I am having trouble with.

How would I write my dll import and call the method?


C++ dll Method declaration
int _stdcall getVBArrayStrValues(LPSTR tagName, LPSTR itemName, 
						short firstElmNo, short lastElmNo, LPSAFEARRAY FAR * ppsaStringRead)

Open in new window


VB Declaration of the dll
Public Declare Function getVBArrayStrValues Lib "FILENAME.dll" _
                                        (ByVal lpTagName As String, _
                                         ByVal lpItemName As String, _
                                         ByVal firstElmNo As Integer, _
                                         ByVal lastElmNo As Integer, _
                                         ByRef stringRead() As vbStringRead) As Integer

Open in new window


Type/Struct Definition
Type vbStringRead
    readStatus As Integer
    valueStr As String
End Type

Open in new window


VB call to the dll
ret = getVBArrayStrValues(tag, item, i1, i2, strRead())

Open in new window



My best guess for the C# Code:

ret = getVBArrayStrValues(tag, item, i1, i2, ref strRead);

Open in new window


public struct vbStringRead
        {
            public int readStatus;
            public string valueStr;
        }

Open in new window


[DllImport("FILENAME.dll")]
        public static extern int getVBArrayStrValues(string lpTagName, string lpItemName, int firstElmNo, int lastElmNo, ref vbStringRead[] stringRead);

Open in new window

0
slavs
Asked:
slavs
2 Solutions
 
Daniel Van Der WerkenCommented:
This Microsoft Article is a good reference for what you want to do. This task is more complicated than you'd think.

Since I don't have your DLL, I cant' try this, but essentially you need to use IntPtr and Marshall stuff.

Per the article:
      // array ByRef  
      int[] array2 = new int[ 10 ];
      int size = array2.Length;
      Console.WriteLine( "\n\nInteger array passed ByRef before call:" );
      for( int i = 0; i < array2.Length; i++ )
      {
         array2[ i ] = i;
         Console.Write( " " + array2[ i ] );
      }
      IntPtr buffer = Marshal.AllocCoTaskMem( Marshal.SizeOf( size ) 
         * array2.Length );
      Marshal.Copy( array2, 0, buffer, array2.Length );

Open in new window


Read the whole article and I think you'll see what you need to do.
0
 
it_saigeDeveloperCommented:
0
 
slavsAuthor Commented:
Thank-you very much to the both of you, great examples/information.
I think I have a good idea of what I have to do now.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now