Re: VB/C Array parameters
I +think+ you want to declare the array ByRef As mw_MatInfoTyp, and then
when you call the function, pass NameOfYourArray(0). Or you might have to
use VarPtr()... Unfortunately, I'm very busy right now, but hopefully this
will be of some help. Good luck!
-- Matthew Solnit
Gastao Woelfert <gastao2000@hotmail.com> wrote in message
news:39afa832$1@news.devx.com...
>
> I have a function in C, that has already been written by other People and
> now I have to write a .dll with this function, so that I can call it from
> the VB 6.0.
> The C-function declaration is as follows:
>
> ---- C-Part----
> -------------------------------------
>
>
> void _stdcall mw_CaeMatinfo(mw_MatInfoTyp *pMatTblToCheck[], unsigned
AnzMatToCheck,
> mw_MatInfoTyp **pMatInfo[], unsigned *AnzMat, mw_ReturnTyp *Ret);
>
> and the structures in C are:
>
> #define MaxSearch 30
> #define MaxResults 200
>
> #define ReturnTypTypeLength 2
> #define ReturnTypIdLength 21
> #define ReturnTypMessageLength 221
> #define ReturnTypLogNoLength 21
> #define ReturnTypLogMsgNoLength 7
> #define ReturnTypMessageV1Length 51
> #define ReturnTypMessageV2Length 51
> #define ReturnTypMessageV3Length 51
> #define ReturnTypMessageV4Length 51
> #define ReturnTypRFC_KeyLength 33
> #define ReturnTypRFC_MessageLength 513
>
> #define MatInfoTypMatnrLength 30
> #define MatInfoTypWerkLength 30
> #define MatInfoTypMaktxLength 70
> #define MatInfoTypValueMentorCLength 31
> #define MatInfoTypValueVisulaC1Length 31
> #define MatInfoTypValueVisulaC2Length 31
> #define MatInfoTypStatusLength 41
> #define MatInfoTypExistenzkzLength 2
> #define MatInfoTypMessageTypeLength 2
> #define MatInfoTypMessageIdLength 21
> #define MatInfoTypMessageLength 221
> #define MatInfoTypMessageV1Length 51
> #define MatInfoTypMessageV2Length 51
> #define MatInfoTypMessageV3Length 51
> #define MatInfoTypMessageV4Length 51
>
>
> typedef struct
> {
> char Type[ReturnTypTypeLength];
> char Id[ReturnTypIdLength];
> unsigned Number;
> char Message[ReturnTypMessageLength];
> char LogNo[ReturnTypLogNoLength];
> char LogMsgNo[ReturnTypLogMsgNoLength];
> char MessageV1[ReturnTypMessageV1Length];
> char MessageV2[ReturnTypMessageV2Length];
> char MessageV3[ReturnTypMessageV3Length];
> char MessageV4[ReturnTypMessageV4Length];
> char RFC_Key[ReturnTypRFC_KeyLength];
> char RFC_Message[ReturnTypRFC_MessageLength];
> } mw_ReturnTyp;
>
> typedef struct
> {
> unsigned Lfdnr;
> char Matnr[MatInfoTypMatnrLength];
> char Werk[MatInfoTypWerkLength];
> char Maktx[MatInfoTypMaktxLength];
> char ValueMentorC[MatInfoTypValueMentorCLength];
> double ValueF;
> char ValueVisulaC1[MatInfoTypValueVisulaC1Length];
> char ValueVisulaC2[MatInfoTypValueVisulaC2Length];
> char Status[MatInfoTypStatusLength];
> char Existenzkz[MatInfoTypExistenzkzLength];
> char MessageType[MatInfoTypMessageTypeLength];
> char MessageId[MatInfoTypMessageIdLength];
> unsigned MessageNumber;
> char Message[MatInfoTypMessageLength];
> char MessageV1[MatInfoTypMessageV1Length];
> char MessageV2[MatInfoTypMessageV2Length];
> char MessageV3[MatInfoTypMessageV3Length];
> char MessageV4[MatInfoTypMessageV4Length];
> } mw_MatInfoTyp;
>
> Here is my program:
> -------------------------------
> VB-Part
>
> ---- Module1.bas----
>
> Option Explicit
>
> Const ReturnTypTypeLength = 2
> Const ReturnTypIdLength = 21
> Const ReturnTypMessageLength = 221
> Const ReturnTypLogNoLength = 21
> Const ReturnTypLogMsgNoLength = 7
> Const ReturnTypMessageV1Length = 51
> Const ReturnTypMessageV2Length = 51
> Const ReturnTypMessageV3Length = 51
> Const ReturnTypMessageV4Length = 51
> Const ReturnTypRFC_KeyLength = 33
> Const ReturnTypRFC_MessageLength = 513
>
>
> Const MatInfoTypMatnrLength = 30
> Const MatInfoTypWerkLength = 30
> Const MatInfoTypMaktxLength = 70
> Const MatInfoTypValueMentorCLength = 31
> Const MatInfoTypValueVisulaC1Length = 31
> Const MatInfoTypValueVisulaC2Length = 31
> Const MatInfoTypStatusLength = 41
> Const MatInfoTypExistenzkzLength = 2
> Const MatInfoTypMessageTypeLength = 2
> Const MatInfoTypMessageIdLength = 21
> Const MatInfoTypMessageLength = 221
> Const MatInfoTypMessageV1Length = 51
> Const MatInfoTypMessageV2Length = 51
> Const MatInfoTypMessageV3Length = 51
> Const MatInfoTypMessageV4Length = 51
>
> Public Type mw_ReturnTyp
> Type As String * ReturnTypTypeLength
> Id As String * ReturnTypIdLength
> Number As Long
> Message As String * ReturnTypMessageLength
> LogNo As String * ReturnTypLogNoLength
> LogMsgNo As String * ReturnTypLogMsgNoLength
> MessageV1 As String * ReturnTypMessageV1Length
> MessageV2 As String * ReturnTypMessageV2Length
> MessageV3 As String * ReturnTypMessageV3Length
> MessageV4 As String * ReturnTypMessageV4Length
> RFC_Key As String * ReturnTypRFC_KeyLength
> RFC_Message As String * ReturnTypRFC_MessageLength
> End Type
>
> Public Type mw_MatInfoTyp
> Lfdnr As Long
> Matnr As String
> Werk As String
> Maktx As String
> ValueMentorC As String
> ValueF As Double
> ValueVisulaC1 As String
> ValueVisulaC2 As String
> Status As String
> Existenzkz As String
> MessageType As String
> MessageId As String
> MessageNumber As Long
> Message As String
> MessageV1 As String
> MessageV2 As String
> MessageV3 As String
> MessageV4 As String
> End Type
>
> Declare Sub mw_CaeMatinfo Lib "ex21c.dll" (pMatTblToCheck() As
mw_MatInfoTyp,
> ByVal AnzMatToCheck As Integer, pMatInfo() As mw_MatInfoTyp, AnzMat As
Integer,
> Ret As mw_ReturnTyp)
>
> (????) I don't know, if that is right. Anyway I had no idea how I should
> declare the double pointer to pMatInfo[] (**pMatInfo[]).
>
> ---- form1.frm ----
>
> Option Explicit
>
> Const MaxSearch = 30
> Const MaxResults = 200
>
> Dim IndexMat As Integer
> Dim IndexWerk As Integer
> Dim pMatTblToCheck(MaxSearch) As mw_MatInfoTyp
> Dim AnzMatToCheck As Integer
> Dim pMatInfo(MaxResults) As mw_MatInfoTyp
> Dim AnzMat As Integer
> Dim Ret As mw_ReturnTyp
>
> Private Sub cmbInMat_Click()
> IndexMat = cmbInMat.ListIndex
> End Sub
>
> Private Sub cmdSearch_Click()
> Dim n
> For n = 0 To cmbInMat.ListCount - 1
> pMatTblToCheck(n).Matnr = cmbInMat.List(n)
> pMatTblToCheck(n).Werk = cmbInWerk.List(n)
> Next
> AnzMatToCheck = cmbInMat.ListCount
> mw_CaeMatinfo pMatTblToCheck(), AnzMatToCheck, pMatInfo(), AnzMat, Ret
> For n = 0 To AnzMat - 1
> cmbOutMat.List(n) = pMatInfo(n).Matnr
> cmbOutWerk.List(n) = pMatInfo(n).Werk
> Next
> End Sub
>
> ------------------------------------------------
>
> I have also tried another things just as a test, to see if I could pass an
> array of structures to a function in a .dll:
>
> ---- C-header test2.h ----
>
> #define FirstNameLength 15
> #define MiddleNameLength 12
> #define LastNameLength 20
>
> typedef struct
> {
> char FirstName[FirstNameLength];
> char MiddleName[MiddleNameLength];
> char LastName[LastNameLength];
> }customer;
>
> void _stdcall mult(customer A[], customer B[]);
>
> ---- C-programm test2.C ----
>
> #include <windows.h>
> #include <stdio.h>
> #include "test2.h"
>
> void _stdcall mult(customer A[], customer B[])
> {
> int i,j;
>
> for(j=0; j<51; j++)
> {
> for(i=0; i<FirstNameLength; i++)
> B[j].FirstName[i] = A[j].FirstName[i];
> for(i=0; i<MiddleNameLength; i++)
> B[j].MiddleName[i] = A[j].MiddleName[i];
> for(i=0; i<LastNameLength; i++)
> B[j].LastName[i] = A[j].LastName[i];
> };
> }
>
> BOOL WINAPI DllMain (HINSTANCE hDLL, DWORD dwReason, LPVOID lpReserved)
> {
> return TRUE;
> }
>
> ---- VB - Modules1.bas ----
>
> Option Explicit
> Declare Sub mult Lib "test2.dll" (A() As Customer, B() As Customer)
> Const FirstNameLength = 15
> Const MiddleNameLength = 12
> Const LastNameLength = 20
> Public Type Customer
> FirstName As String * FirstNameLength
> MiddleName As String * MiddleNameLength
> LastName As String * LastNameLength
> End Type
> Global x() As Customer, y() As Customer
>
> ---- VB - Form1.frm ---
>
> Private Sub Command1_Click()
> Dim i As Integer
> ReDim x(50)
> ReDim y(50)
> For i = 0 To 10
> x(i).FirstName = Text1.Text
> x(i).MiddleName = Text2.Text
> x(i).LastName = Text3.Text
> Next
> 'I have also called the function with different input values in
> each structure
> mult x(), y()
> End Sub
>
> ---------------------------------------
>
> That has worked. With the debug, I can see that I got the right results,
> at least when the array is small (size about 50). But when I close the
VB-program
> (when I'm using the VB-debug or running the executable file), an error
ocurrs
> (unallowed memory handling, or something like that - my Windows is not in
> english -) and the program or the VB-studio shuts down.
> When I use bigger arrays, the same error occurs already when call the
function.
> Then when I change:
>
> void _stdcall mult(customer A[], customer B[]);
>
> for :
>
> void _stdcall mult(customer *A[], customer *B[]);
>
> , because that is what I need in my program, that the same error occurs
already
> when call the function.
>
> Is there any way of passing pointers of arrays of structures from the VB
> to a function in a .dll wrutten in C????
>
> I would appreciate your help. Thanks in advance
>
> Best Regards,
>
> Gastao Woelfert
Re: VB/C Array parameters
I +think+ you want to declare the array ByRef As mw_MatInfoTyp, and then
when you call the function, pass NameOfYourArray(0). Or you might have to
use VarPtr()... Unfortunately, I'm very busy right now, but hopefully this
will be of some help. Good luck!
-- Matthew Solnit
Gastao Woelfert <gastao2000@hotmail.com> wrote in message
news:39afa832$1@news.devx.com...
>
> I have a function in C, that has already been written by other People and
> now I have to write a .dll with this function, so that I can call it from
> the VB 6.0.
> The C-function declaration is as follows:
>
> ---- C-Part----
> -------------------------------------
>
>
> void _stdcall mw_CaeMatinfo(mw_MatInfoTyp *pMatTblToCheck[], unsigned
AnzMatToCheck,
> mw_MatInfoTyp **pMatInfo[], unsigned *AnzMat, mw_ReturnTyp *Ret);
>
> and the structures in C are:
>
> #define MaxSearch 30
> #define MaxResults 200
>
> #define ReturnTypTypeLength 2
> #define ReturnTypIdLength 21
> #define ReturnTypMessageLength 221
> #define ReturnTypLogNoLength 21
> #define ReturnTypLogMsgNoLength 7
> #define ReturnTypMessageV1Length 51
> #define ReturnTypMessageV2Length 51
> #define ReturnTypMessageV3Length 51
> #define ReturnTypMessageV4Length 51
> #define ReturnTypRFC_KeyLength 33
> #define ReturnTypRFC_MessageLength 513
>
> #define MatInfoTypMatnrLength 30
> #define MatInfoTypWerkLength 30
> #define MatInfoTypMaktxLength 70
> #define MatInfoTypValueMentorCLength 31
> #define MatInfoTypValueVisulaC1Length 31
> #define MatInfoTypValueVisulaC2Length 31
> #define MatInfoTypStatusLength 41
> #define MatInfoTypExistenzkzLength 2
> #define MatInfoTypMessageTypeLength 2
> #define MatInfoTypMessageIdLength 21
> #define MatInfoTypMessageLength 221
> #define MatInfoTypMessageV1Length 51
> #define MatInfoTypMessageV2Length 51
> #define MatInfoTypMessageV3Length 51
> #define MatInfoTypMessageV4Length 51
>
>
> typedef struct
> {
> char Type[ReturnTypTypeLength];
> char Id[ReturnTypIdLength];
> unsigned Number;
> char Message[ReturnTypMessageLength];
> char LogNo[ReturnTypLogNoLength];
> char LogMsgNo[ReturnTypLogMsgNoLength];
> char MessageV1[ReturnTypMessageV1Length];
> char MessageV2[ReturnTypMessageV2Length];
> char MessageV3[ReturnTypMessageV3Length];
> char MessageV4[ReturnTypMessageV4Length];
> char RFC_Key[ReturnTypRFC_KeyLength];
> char RFC_Message[ReturnTypRFC_MessageLength];
> } mw_ReturnTyp;
>
> typedef struct
> {
> unsigned Lfdnr;
> char Matnr[MatInfoTypMatnrLength];
> char Werk[MatInfoTypWerkLength];
> char Maktx[MatInfoTypMaktxLength];
> char ValueMentorC[MatInfoTypValueMentorCLength];
> double ValueF;
> char ValueVisulaC1[MatInfoTypValueVisulaC1Length];
> char ValueVisulaC2[MatInfoTypValueVisulaC2Length];
> char Status[MatInfoTypStatusLength];
> char Existenzkz[MatInfoTypExistenzkzLength];
> char MessageType[MatInfoTypMessageTypeLength];
> char MessageId[MatInfoTypMessageIdLength];
> unsigned MessageNumber;
> char Message[MatInfoTypMessageLength];
> char MessageV1[MatInfoTypMessageV1Length];
> char MessageV2[MatInfoTypMessageV2Length];
> char MessageV3[MatInfoTypMessageV3Length];
> char MessageV4[MatInfoTypMessageV4Length];
> } mw_MatInfoTyp;
>
> Here is my program:
> -------------------------------
> VB-Part
>
> ---- Module1.bas----
>
> Option Explicit
>
> Const ReturnTypTypeLength = 2
> Const ReturnTypIdLength = 21
> Const ReturnTypMessageLength = 221
> Const ReturnTypLogNoLength = 21
> Const ReturnTypLogMsgNoLength = 7
> Const ReturnTypMessageV1Length = 51
> Const ReturnTypMessageV2Length = 51
> Const ReturnTypMessageV3Length = 51
> Const ReturnTypMessageV4Length = 51
> Const ReturnTypRFC_KeyLength = 33
> Const ReturnTypRFC_MessageLength = 513
>
>
> Const MatInfoTypMatnrLength = 30
> Const MatInfoTypWerkLength = 30
> Const MatInfoTypMaktxLength = 70
> Const MatInfoTypValueMentorCLength = 31
> Const MatInfoTypValueVisulaC1Length = 31
> Const MatInfoTypValueVisulaC2Length = 31
> Const MatInfoTypStatusLength = 41
> Const MatInfoTypExistenzkzLength = 2
> Const MatInfoTypMessageTypeLength = 2
> Const MatInfoTypMessageIdLength = 21
> Const MatInfoTypMessageLength = 221
> Const MatInfoTypMessageV1Length = 51
> Const MatInfoTypMessageV2Length = 51
> Const MatInfoTypMessageV3Length = 51
> Const MatInfoTypMessageV4Length = 51
>
> Public Type mw_ReturnTyp
> Type As String * ReturnTypTypeLength
> Id As String * ReturnTypIdLength
> Number As Long
> Message As String * ReturnTypMessageLength
> LogNo As String * ReturnTypLogNoLength
> LogMsgNo As String * ReturnTypLogMsgNoLength
> MessageV1 As String * ReturnTypMessageV1Length
> MessageV2 As String * ReturnTypMessageV2Length
> MessageV3 As String * ReturnTypMessageV3Length
> MessageV4 As String * ReturnTypMessageV4Length
> RFC_Key As String * ReturnTypRFC_KeyLength
> RFC_Message As String * ReturnTypRFC_MessageLength
> End Type
>
> Public Type mw_MatInfoTyp
> Lfdnr As Long
> Matnr As String
> Werk As String
> Maktx As String
> ValueMentorC As String
> ValueF As Double
> ValueVisulaC1 As String
> ValueVisulaC2 As String
> Status As String
> Existenzkz As String
> MessageType As String
> MessageId As String
> MessageNumber As Long
> Message As String
> MessageV1 As String
> MessageV2 As String
> MessageV3 As String
> MessageV4 As String
> End Type
>
> Declare Sub mw_CaeMatinfo Lib "ex21c.dll" (pMatTblToCheck() As
mw_MatInfoTyp,
> ByVal AnzMatToCheck As Integer, pMatInfo() As mw_MatInfoTyp, AnzMat As
Integer,
> Ret As mw_ReturnTyp)
>
> (????) I don't know, if that is right. Anyway I had no idea how I should
> declare the double pointer to pMatInfo[] (**pMatInfo[]).
>
> ---- form1.frm ----
>
> Option Explicit
>
> Const MaxSearch = 30
> Const MaxResults = 200
>
> Dim IndexMat As Integer
> Dim IndexWerk As Integer
> Dim pMatTblToCheck(MaxSearch) As mw_MatInfoTyp
> Dim AnzMatToCheck As Integer
> Dim pMatInfo(MaxResults) As mw_MatInfoTyp
> Dim AnzMat As Integer
> Dim Ret As mw_ReturnTyp
>
> Private Sub cmbInMat_Click()
> IndexMat = cmbInMat.ListIndex
> End Sub
>
> Private Sub cmdSearch_Click()
> Dim n
> For n = 0 To cmbInMat.ListCount - 1
> pMatTblToCheck(n).Matnr = cmbInMat.List(n)
> pMatTblToCheck(n).Werk = cmbInWerk.List(n)
> Next
> AnzMatToCheck = cmbInMat.ListCount
> mw_CaeMatinfo pMatTblToCheck(), AnzMatToCheck, pMatInfo(), AnzMat, Ret
> For n = 0 To AnzMat - 1
> cmbOutMat.List(n) = pMatInfo(n).Matnr
> cmbOutWerk.List(n) = pMatInfo(n).Werk
> Next
> End Sub
>
> ------------------------------------------------
>
> I have also tried another things just as a test, to see if I could pass an
> array of structures to a function in a .dll:
>
> ---- C-header test2.h ----
>
> #define FirstNameLength 15
> #define MiddleNameLength 12
> #define LastNameLength 20
>
> typedef struct
> {
> char FirstName[FirstNameLength];
> char MiddleName[MiddleNameLength];
> char LastName[LastNameLength];
> }customer;
>
> void _stdcall mult(customer A[], customer B[]);
>
> ---- C-programm test2.C ----
>
> #include <windows.h>
> #include <stdio.h>
> #include "test2.h"
>
> void _stdcall mult(customer A[], customer B[])
> {
> int i,j;
>
> for(j=0; j<51; j++)
> {
> for(i=0; i<FirstNameLength; i++)
> B[j].FirstName[i] = A[j].FirstName[i];
> for(i=0; i<MiddleNameLength; i++)
> B[j].MiddleName[i] = A[j].MiddleName[i];
> for(i=0; i<LastNameLength; i++)
> B[j].LastName[i] = A[j].LastName[i];
> };
> }
>
> BOOL WINAPI DllMain (HINSTANCE hDLL, DWORD dwReason, LPVOID lpReserved)
> {
> return TRUE;
> }
>
> ---- VB - Modules1.bas ----
>
> Option Explicit
> Declare Sub mult Lib "test2.dll" (A() As Customer, B() As Customer)
> Const FirstNameLength = 15
> Const MiddleNameLength = 12
> Const LastNameLength = 20
> Public Type Customer
> FirstName As String * FirstNameLength
> MiddleName As String * MiddleNameLength
> LastName As String * LastNameLength
> End Type
> Global x() As Customer, y() As Customer
>
> ---- VB - Form1.frm ---
>
> Private Sub Command1_Click()
> Dim i As Integer
> ReDim x(50)
> ReDim y(50)
> For i = 0 To 10
> x(i).FirstName = Text1.Text
> x(i).MiddleName = Text2.Text
> x(i).LastName = Text3.Text
> Next
> 'I have also called the function with different input values in
> each structure
> mult x(), y()
> End Sub
>
> ---------------------------------------
>
> That has worked. With the debug, I can see that I got the right results,
> at least when the array is small (size about 50). But when I close the
VB-program
> (when I'm using the VB-debug or running the executable file), an error
ocurrs
> (unallowed memory handling, or something like that - my Windows is not in
> english -) and the program or the VB-studio shuts down.
> When I use bigger arrays, the same error occurs already when call the
function.
> Then when I change:
>
> void _stdcall mult(customer A[], customer B[]);
>
> for :
>
> void _stdcall mult(customer *A[], customer *B[]);
>
> , because that is what I need in my program, that the same error occurs
already
> when call the function.
>
> Is there any way of passing pointers of arrays of structures from the VB
> to a function in a .dll wrutten in C????
>
> I would appreciate your help. Thanks in advance
>
> Best Regards,
>
> Gastao Woelfert