Structure conversion from C++ to VB-2008?

  • Thread starter Thread starter Klatuu
  • Start date Start date
K

Klatuu

Hi ,

I'm trying to write a DLL for an application using VB-2008. The DevKit for
the DLL is
written for C/C++ and I'm trying to translate this into VB-2008. Where I
think I need help is these structures specified by the application for C++
programmers writing DLL in C++. Is it possible to convert these into
VB-2008?

There's a brief description of AmiVar structure first then the second
structure PlugInInfo in their DevKit. Below that is my attempt at the
conversion and my questions about it's conversion:


Thanks for your help,
Klatuu
----------------------------------------------------------=======================----------------------------------============================-------------------------------


Dev Kit Instructions
----------------------------------------------------------=======================----------------------------------============================-------------------------------


2.2 INTERFACE DEFINITION

2.2.1 Data types

One of the most important structures is AmiVar structure. It is used for
holding different types of values. This single structure can hold floating
point number, the array of floating point numbers, a string or IDispatch
interface pointer. Each AFL function receives its arguments as an array of
AmiVar values. The AmiVar structure looks like this:

typedef struct AmiVar
{
int type;
union
{
float val;
float *array;
char *string;
void *disp;
};
} AmiVar;
The first member of the structure - type - holds the information about data
type which can be one of the following:

// the list of AmiVar types
enum { VAR_NONE, VAR_FLOAT, VAR_ARRAY, VAR_STRING, VAR_DISPATCH };
VAR_NONE represents the AmiVar that does not have any value. If type is
VAR_FLOAT it means that val member is valid and hold the floating point
number. If type equals VAR_ARRAY it means that array member is valid and
points to the array of floating point values (the size of the array is the
same for all arrays during single function call and can be obtained from
site interface - which will be described later). If type equals to
VAR_STRING it means that string member is valid and points to
null-terminated C-style character string. The VAR_DISPATCH type is provided
for calling COM objects and will not be covered here.

Proper usage of AmiVar structure looks like this:

AmiVar myvar;
myvar.type = VAR_FLOAT; // set the type
myvar.val = 10.5f; // assign floating point numberPlease note that
assigning arrays and strings usually require allocating memory. These
allocations must be performed using special allocator functions provided by
the site interface in order to enable AmiBroker to track these allocations
and free the memory when it is no longer needed.


The next important structure is the SiteInterface:

struct SiteInterface { int nStructSize; int (*GetArraySize)
(void); float * (*GetStockArray)( int nType ); AmiVar(*GetVariable)(
const char *pszName ); void (*SetVariable) ( const char *pszName,
AmiVar newValue ); AmiVar (*CallFunction) ( const char *szName, int
nNumArgs, AmiVar *ArgsTable ); AmiVar (*AllocArrayResult) (void);void*
(*Alloc) (unsigned int nSize); void (*Free) (void *pMemory);};




----------------------------------------------------------=======================----------------------------------============================-------------------------------
----------------------------------------------------------=======================----------------------------------============================-------------------------------

I'm having a bit of difficulty with this Structure AmiVar, specifically how
to translate it from C++ to VB. I'm a little confused with the NAMES of the
Variables in the UNION as well as the TYPES in the union. I can create a
UNION structure with the StructLayout identifier, and then allow the members
to use the same memory space with the FieldOffset identifier. However, the
naming and types of the members is where I'm having problems.

The first structure member "var" makes sense, yet the next member "*array"
doesn't. I read "*array" as a pointer to an array of type Single Precision
Floating point numbers, however, I'm not allowed to call it '*array'. Yet
this might work:

Public array() As Single



There's a similar issue is the next member *string.

Public *string As String

'string' will not work because it is a reserved word.


And finally the PluginInfo structure:

The issue I have here is putting the two constants into the structure:

Dim 13012679 as Integer
Dim 387000

Do these constants they have member names?

Thanks,
Tracy






Here's my VB-2008 template so far:

' ==========================================================
' ----------------------------------------------------------
' This Import statement is used to implement the Union structure
Imports System.Runtime.InteropServices

Public Class dvClass

Public Const PLUGIN_NAME = "DV Functions Plugin"
Public Const VENDOR_NAME = "tdc"
Public Const PLUGIN_VERSION = 10000
Public Const THIS_PLUGIN_TYPE = 3


' ----------------------------------------------------------
<StructLayout(LayoutKind.Explicit)> Public Structure AmiVar
Dim type As Integer

<FieldOffset(0)> Public val As Single
<FieldOffset(0)> Public *array as Single
<FieldOffset(0)> Public *string as String
<FieldOffset(0)> Public *disp as void

End Structure


' ----------------------------------------------------------
Public Structure PluginInfo
Dim size As IntPtr = Marshal.SizeOf(PluginInfo)
Dim THIS_PLUGIN_TYPE As Integer
Dim PLUGIN_VERSION As Integer
Dim PIDCODE As Enumerable =( 'm', 'o', 'c', 'a')
Dim PLUGIN_NAME As String
Dim VENDOR_NAME As String
Dim 13012679 as Integer
Dim 387000
End Structure

' ----------------------------------------------------------
Public Shared Function GetPluginInfo(ByRef dvPlugInInfo As PluginInfo) As
Integer

dvPlugInInfo.PLUGIN_NAME = PLUGIN_NAME
dvPlugInInfo.VENDOR_NAME = VENDOR_NAME
' Use what is needed here for type
dvPlugInInfo.THIS_PLUGIN_TYPE = 3
dvPlugInInfo.PLUGIN_VERSION = PLUGIN_VERSION

GetPluginInfo = 1
End Function

' ----------------------------------------------------------
Public Shared Function Init()
Init = 1
End Function

' ----------------------------------------------------------
Public Shared Function Release()
Release = 1
End Function

' ----------------------------------------------------------
Public Shared Function SetSiteInterface()
SetSiteInterface = 1
End Function


' ----------------------------------------------------------
Public Shared Function GetFunctionTable()
GetFunctionTable = 1
End Function

' ----------------------------------------------------------
Public Function IIR2(ByVal InputArray() As Object, ByVal f0 As Object,
ByVal f1 As Object, ByVal f2 As Object) As Object

Dim Result()

ReDim Result(UBound(InputArray)) ' size the Result array to match
InputArray

'initialize first two elements

Result(0) = InputArray(0)
Result(1) = InputArray(1)

For i = 2 To UBound(InputArray)

Result(i) = f0 * InputArray(i) + f1 * Result(i - 1) + f2 *
Result(i - 2)

Next

IIR2 = Result

End Function
End Class
======================================================
 
Klatuu said:
Hi ,

I'm trying to write a DLL for an application using VB-2008. The DevKit for
the DLL is
written for C/C++ and I'm trying to translate this into VB-2008. Where I
think I need help is these structures specified by the application for C++
programmers writing DLL in C++. Is it possible to convert these into
VB-2008?

There's a brief description of AmiVar structure first then the second
structure PlugInInfo in their DevKit. Below that is my attempt at the
conversion and my questions about it's conversion:


Thanks for your help,
Klatuu
----------------------------------------------------------=======================----------------------------------============================-------------------------------


Dev Kit Instructions
----------------------------------------------------------=======================----------------------------------============================-------------------------------


2.2 INTERFACE DEFINITION

2.2.1 Data types

One of the most important structures is AmiVar structure. It is used for
holding different types of values. This single structure can hold floating
point number, the array of floating point numbers, a string or IDispatch
interface pointer. Each AFL function receives its arguments as an array of
AmiVar values. The AmiVar structure looks like this:

typedef struct AmiVar
{
int type;
union
{
float val;
float *array;
char *string;
void *disp;
};
} AmiVar;
The first member of the structure - type - holds the information about
data
type which can be one of the following:

// the list of AmiVar types
enum { VAR_NONE, VAR_FLOAT, VAR_ARRAY, VAR_STRING, VAR_DISPATCH };
VAR_NONE represents the AmiVar that does not have any value. If type is
VAR_FLOAT it means that val member is valid and hold the floating point
number. If type equals VAR_ARRAY it means that array member is valid and
points to the array of floating point values (the size of the array is the
same for all arrays during single function call and can be obtained from
site interface - which will be described later). If type equals to
VAR_STRING it means that string member is valid and points to
null-terminated C-style character string. The VAR_DISPATCH type is
provided
for calling COM objects and will not be covered here.

Proper usage of AmiVar structure looks like this:

AmiVar myvar;
myvar.type = VAR_FLOAT; // set the type
myvar.val = 10.5f; // assign floating point numberPlease note that
assigning arrays and strings usually require allocating memory. These
allocations must be performed using special allocator functions provided
by
the site interface in order to enable AmiBroker to track these allocations
and free the memory when it is no longer needed.


The next important structure is the SiteInterface:

struct SiteInterface { int nStructSize; int (*GetArraySize)
(void); float * (*GetStockArray)( int nType ); AmiVar(*GetVariable)(
const char *pszName ); void (*SetVariable) ( const char *pszName,
AmiVar newValue ); AmiVar (*CallFunction) ( const char *szName, int
nNumArgs, AmiVar *ArgsTable ); AmiVar (*AllocArrayResult) (void);void*
(*Alloc) (unsigned int nSize); void (*Free) (void *pMemory);};




----------------------------------------------------------=======================----------------------------------============================-------------------------------
----------------------------------------------------------=======================----------------------------------============================-------------------------------

I'm having a bit of difficulty with this Structure AmiVar, specifically
how
to translate it from C++ to VB. I'm a little confused with the NAMES of
the
Variables in the UNION as well as the TYPES in the union. I can create a
UNION structure with the StructLayout identifier, and then allow the
members
to use the same memory space with the FieldOffset identifier. However, the
naming and types of the members is where I'm having problems.

The first structure member "var" makes sense, yet the next member "*array"
doesn't. I read "*array" as a pointer to an array of type Single Precision
Floating point numbers, however, I'm not allowed to call it '*array'. Yet
this might work:

Public array() As Single



There's a similar issue is the next member *string.

Public *string As String

'string' will not work because it is a reserved word.


And finally the PluginInfo structure:

The issue I have here is putting the two constants into the structure:

Dim 13012679 as Integer
Dim 387000

Do these constants they have member names?

Thanks,
Tracy






Here's my VB-2008 template so far:

' ==========================================================
' ----------------------------------------------------------
' This Import statement is used to implement the Union structure
Imports System.Runtime.InteropServices

Public Class dvClass

Public Const PLUGIN_NAME = "DV Functions Plugin"
Public Const VENDOR_NAME = "tdc"
Public Const PLUGIN_VERSION = 10000
Public Const THIS_PLUGIN_TYPE = 3


' ----------------------------------------------------------
<StructLayout(LayoutKind.Explicit)> Public Structure AmiVar
Dim type As Integer

<FieldOffset(0)> Public val As Single
<FieldOffset(0)> Public *array as Single
<FieldOffset(0)> Public *string as String
<FieldOffset(0)> Public *disp as void

End Structure


' ----------------------------------------------------------
Public Structure PluginInfo
Dim size As IntPtr = Marshal.SizeOf(PluginInfo)
Dim THIS_PLUGIN_TYPE As Integer
Dim PLUGIN_VERSION As Integer
Dim PIDCODE As Enumerable =( 'm', 'o', 'c', 'a')
Dim PLUGIN_NAME As String
Dim VENDOR_NAME As String
Dim 13012679 as Integer
Dim 387000
End Structure

' ----------------------------------------------------------
Public Shared Function GetPluginInfo(ByRef dvPlugInInfo As PluginInfo)
As
Integer

dvPlugInInfo.PLUGIN_NAME = PLUGIN_NAME
dvPlugInInfo.VENDOR_NAME = VENDOR_NAME
' Use what is needed here for type
dvPlugInInfo.THIS_PLUGIN_TYPE = 3
dvPlugInInfo.PLUGIN_VERSION = PLUGIN_VERSION

GetPluginInfo = 1
End Function

' ----------------------------------------------------------
Public Shared Function Init()
Init = 1
End Function

' ----------------------------------------------------------
Public Shared Function Release()
Release = 1
End Function

' ----------------------------------------------------------
Public Shared Function SetSiteInterface()
SetSiteInterface = 1
End Function


' ----------------------------------------------------------
Public Shared Function GetFunctionTable()
GetFunctionTable = 1
End Function

' ----------------------------------------------------------
Public Function IIR2(ByVal InputArray() As Object, ByVal f0 As Object,
ByVal f1 As Object, ByVal f2 As Object) As Object

Dim Result()

ReDim Result(UBound(InputArray)) ' size the Result array to match
InputArray

'initialize first two elements

Result(0) = InputArray(0)
Result(1) = InputArray(1)

For i = 2 To UBound(InputArray)

Result(i) = f0 * InputArray(i) + f1 * Result(i - 1) + f2 *
Result(i - 2)

Next

IIR2 = Result

End Function
End Class
======================================================
You're going to have "interesting times" with this. The first example you
provide includes a union and pointers, neither of which are supported in VB.
 
The issue with reserved words is a non-issue - just enclose the identifier in
square brackets:
e.g.,
Public [Array]() As Single
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert VB to C#, C++, or Java
Convert C# to VB, C++, or Java
Convert C++ to C#, VB, or Java
Convert Java to C#, C++, or VB
 
PvdG42:

Thanks for your reply. I'm wondering about this Structure using the
StructureLayout & Field offset parameters. Isn't this a VB solution to the
Unions issue? Is it equvalent to the C++ structure & Union below it?

----------------------------------------------------------
<StructLayout(LayoutKind.Explicit)> Public Structure AmiVar
Dim type As Integer

<FieldOffset(0)> Public val As Single
<FieldOffset(0)> Public *array as Single
<FieldOffset(0)> Public *string as String
<FieldOffset(0)> Public *disp as void

End Structure
----------------------------------------------------------


typedef struct AmiVar
{
int type;
union
{
float val;
float *array;
char *string;
void *disp;
};
} AmiVar;



Personally, I'm having more of an issue with PlugInInfo:


His definition is this:
=================================
static struct PluginInfo oPluginInfo =
{
sizeof( struct PluginInfo ),
THIS_PLUGIN_TYPE,
PLUGIN_VERSION,
PIDCODE( 'm', 'o', 'c', 'a'),
PLUGIN_NAME,
VENDOR_NAME,
13012679, <<========
387000 <<========
};

======================

My translation has an issue with the two constants (plain numbers) near the
end of the struct?

Is this possible to do in VB? Why would anyone need these constants in their
structure anyway?
Thanks,
Klatuu
 
Thanks David. That's great to know.

Klatuu





David Anton said:
The issue with reserved words is a non-issue - just enclose the identifier in
square brackets:
e.g.,
Public [Array]() As Single
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert VB to C#, C++, or Java
Convert C# to VB, C++, or Java
Convert C++ to C#, VB, or Java
Convert Java to C#, C++, or VB


Klatuu said:
Hi ,

I'm trying to write a DLL for an application using VB-2008. The DevKit for
the DLL is
written for C/C++ and I'm trying to translate this into VB-2008. Where I
think I need help is these structures specified by the application for C++
programmers writing DLL in C++. Is it possible to convert these into
VB-2008?

There's a brief description of AmiVar structure first then the second
structure PlugInInfo in their DevKit. Below that is my attempt at the
conversion and my questions about it's conversion:


Thanks for your help,
Klatuu
----------------------------------------------------------=======================----------------------------------============================-------------------------------


Dev Kit Instructions
----------------------------------------------------------=======================----------------------------------============================-------------------------------


2.2 INTERFACE DEFINITION

2.2.1 Data types

One of the most important structures is AmiVar structure. It is used for
holding different types of values. This single structure can hold floating
point number, the array of floating point numbers, a string or IDispatch
interface pointer. Each AFL function receives its arguments as an array of
AmiVar values. The AmiVar structure looks like this:

typedef struct AmiVar
{
int type;
union
{
float val;
float *array;
char *string;
void *disp;
};
} AmiVar;
The first member of the structure - type - holds the information about data
type which can be one of the following:

// the list of AmiVar types
enum { VAR_NONE, VAR_FLOAT, VAR_ARRAY, VAR_STRING, VAR_DISPATCH };
VAR_NONE represents the AmiVar that does not have any value. If type is
VAR_FLOAT it means that val member is valid and hold the floating point
number. If type equals VAR_ARRAY it means that array member is valid and
points to the array of floating point values (the size of the array is the
same for all arrays during single function call and can be obtained from
site interface - which will be described later). If type equals to
VAR_STRING it means that string member is valid and points to
null-terminated C-style character string. The VAR_DISPATCH type is provided
for calling COM objects and will not be covered here.

Proper usage of AmiVar structure looks like this:

AmiVar myvar;
myvar.type = VAR_FLOAT; // set the type
myvar.val = 10.5f; // assign floating point numberPlease note that
assigning arrays and strings usually require allocating memory. These
allocations must be performed using special allocator functions provided by
the site interface in order to enable AmiBroker to track these allocations
and free the memory when it is no longer needed.


The next important structure is the SiteInterface:

struct SiteInterface { int nStructSize; int (*GetArraySize)
(void); float * (*GetStockArray)( int nType ); AmiVar(*GetVariable)(
const char *pszName ); void (*SetVariable) ( const char *pszName,
AmiVar newValue ); AmiVar (*CallFunction) ( const char *szName, int
nNumArgs, AmiVar *ArgsTable ); AmiVar (*AllocArrayResult) (void);void*
(*Alloc) (unsigned int nSize); void (*Free) (void *pMemory);};




----------------------------------------------------------=======================----------------------------------============================-------------------------------
----------------------------------------------------------=======================----------------------------------============================-------------------------------

I'm having a bit of difficulty with this Structure AmiVar, specifically how
to translate it from C++ to VB. I'm a little confused with the NAMES of the
Variables in the UNION as well as the TYPES in the union. I can create a
UNION structure with the StructLayout identifier, and then allow the members
to use the same memory space with the FieldOffset identifier. However, the
naming and types of the members is where I'm having problems.

The first structure member "var" makes sense, yet the next member "*array"
doesn't. I read "*array" as a pointer to an array of type Single Precision
Floating point numbers, however, I'm not allowed to call it '*array'. Yet
this might work:

Public array() As Single



There's a similar issue is the next member *string.

Public *string As String

'string' will not work because it is a reserved word.


And finally the PluginInfo structure:

The issue I have here is putting the two constants into the structure:

Dim 13012679 as Integer
Dim 387000

Do these constants they have member names?

Thanks,
Tracy






Here's my VB-2008 template so far:

' ==========================================================
' ----------------------------------------------------------
' This Import statement is used to implement the Union structure
Imports System.Runtime.InteropServices

Public Class dvClass

Public Const PLUGIN_NAME = "DV Functions Plugin"
Public Const VENDOR_NAME = "tdc"
Public Const PLUGIN_VERSION = 10000
Public Const THIS_PLUGIN_TYPE = 3


' ----------------------------------------------------------
<StructLayout(LayoutKind.Explicit)> Public Structure AmiVar
Dim type As Integer

<FieldOffset(0)> Public val As Single
<FieldOffset(0)> Public *array as Single
<FieldOffset(0)> Public *string as String
<FieldOffset(0)> Public *disp as void

End Structure


' ----------------------------------------------------------
Public Structure PluginInfo
Dim size As IntPtr = Marshal.SizeOf(PluginInfo)
Dim THIS_PLUGIN_TYPE As Integer
Dim PLUGIN_VERSION As Integer
Dim PIDCODE As Enumerable =( 'm', 'o', 'c', 'a')
Dim PLUGIN_NAME As String
Dim VENDOR_NAME As String
Dim 13012679 as Integer
Dim 387000
End Structure

' ----------------------------------------------------------
Public Shared Function GetPluginInfo(ByRef dvPlugInInfo As PluginInfo) As
Integer

dvPlugInInfo.PLUGIN_NAME = PLUGIN_NAME
dvPlugInInfo.VENDOR_NAME = VENDOR_NAME
' Use what is needed here for type
dvPlugInInfo.THIS_PLUGIN_TYPE = 3
dvPlugInInfo.PLUGIN_VERSION = PLUGIN_VERSION

GetPluginInfo = 1
End Function

' ----------------------------------------------------------
Public Shared Function Init()
Init = 1
End Function

' ----------------------------------------------------------
Public Shared Function Release()
Release = 1
End Function

' ----------------------------------------------------------
Public Shared Function SetSiteInterface()
SetSiteInterface = 1
End Function


' ----------------------------------------------------------
Public Shared Function GetFunctionTable()
GetFunctionTable = 1
End Function

' ----------------------------------------------------------
Public Function IIR2(ByVal InputArray() As Object, ByVal f0 As Object,
ByVal f1 As Object, ByVal f2 As Object) As Object

Dim Result()

ReDim Result(UBound(InputArray)) ' size the Result array to match
InputArray

'initialize first two elements

Result(0) = InputArray(0)
Result(1) = InputArray(1)

For i = 2 To UBound(InputArray)

Result(i) = f0 * InputArray(i) + f1 * Result(i - 1) + f2 *
Result(i - 2)

Next

IIR2 = Result

End Function
End Class
======================================================
 
Back
Top