Solved

Converting VB6 to C#

Posted on 2014-12-09
3
192 Views
Last Modified: 2014-12-10
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
Comment
Question by:slavs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 20

Assisted Solution

by:Daniel Van Der Werken
Daniel Van Der Werken earned 250 total points
ID: 40488783
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
 
LVL 34

Accepted Solution

by:
it_saige earned 250 total points
ID: 40489313
0
 
LVL 2

Author Closing Comment

by:slavs
ID: 40492447
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses
Course of the Month8 days, 13 hours left to enroll

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question