Calling a C++ DLL using vb 
Author Message
 Calling a C++ DLL using vb

Is there any examples of calling a C++ DLL in vb and calling the exported
functions?

I'm not a vb programmer I'm just trying to help one of our developers out.

Thanks.



Mon, 23 May 2005 07:01:53 GMT  
 Calling a C++ DLL using vb
//
// CPP code
//

#include <windows.h>
#include <stdio.h>

typedef struct
    {
        int                  AccessStatus;
        int                  UserLoginID;
        BYTE                 abytCapability [ 128 ];
        BYTE                 abytVoicePath [ 128 ];
        BYTE                 abytPassword [ 20 ];
        BYTE                 abytUserName [ 20 ];
        }   TypeTestPassingUDT;

extern "C"  __declspec(dllexport) DWORD __stdcall
TestPassing

    (TypeTestPassingUDT     * pUserData)

{
char                           szTemp [ 128 ];

memcpy (szTemp,
        (char *) pUserData->abytUserName,
        16);
szTemp [ 16 ] = 0;
printf ("DLL UserName <%s>\n",szTemp);

memcpy (szTemp,
        (char *) pUserData->abytPassword,
        16);
szTemp [ 16 ] = 0;
printf ("DLL Password <%s>\n",szTemp);

sprintf ((char *) pUserData->abytUserName,
         "{HI-%s}",
         szTemp);
strcpy ((char *) pUserData->abytVoicePath,
        "J:VoicePath");
strcpy ((char *) pUserData->abytCapability,
        "This is the capability");
pUserData->AccessStatus = 0x12345678;
pUserData->UserLoginID = 0xFEDCBA98;
return 0xAABBCCDD;

Quote:
}   // TestPassing ()

#
# DLL def file
#
LIBRARY TestPassingUDT
EXPORTS

'
' VB7 code
'
Imports System
Imports System.Text
Imports System.Runtime.InteropServices

<StructLayout(LayoutKind.Sequential)> Public Structure Foo
    Public AccessStatus As Integer
    Public UserLoginID As Integer
    <MarshalAs(UnmanagedType.ByValArray, SizeConst:=128)>
Public Capability() As Byte
    <MarshalAs(UnmanagedType.ByValArray, SizeConst:=128)>
Public VoicePath() As Byte
    <MarshalAs(UnmanagedType.ByValArray, SizeConst:=20)>
Public Password() As Byte
    <MarshalAs(UnmanagedType.ByValArray, SizeConst:=20)>
Public UserName() As Byte
    Public Sub New(ByVal intUnsage As Integer)
        ReDim Capability(128)
        ReDim VoicePath(128)
        ReDim UserName(20)
        ReDim Password(20)
    End Sub
End Structure

Public Module TestingPassingUDT

    <DllImport("c:\pat\exe\testpassingudt")> Private
Function _
        TestPassing(ByRef f As Foo) As Integer
    End Function

    Public Sub Main()
        Dim f As Foo = New Foo(0)
        Dim status As Integer
        Dim strData As String
        Dim intPosition As Integer
        f.AccessStatus = 10
        f.UserLoginID = 255
        Encoding.ASCII.GetBytes("Hello world".ToCharArray
(), 0, 11, f.UserName, 0)
        Encoding.ASCII.GetBytes("My Password".ToCharArray
(), 0, 11, f.Password, 0)
        f.UserName(15) = 0
        status = TestPassing(f)
        Console.WriteLine("CS {0:X}", status)
        strData = Encoding.ASCII.GetString(f.UserName)
        intPosition = strData.IndexOf(Chr(0))
        strData = strData.Substring(0, intPosition)
        Console.WriteLine("UN ={0}=", strData)
        Console.WriteLine("UN <{0}>",
Encoding.ASCII.GetString(f.UserName))
        Console.WriteLine("PW <{0}>",
Encoding.ASCII.GetString(f.Password))
        Console.WriteLine("VP <{0}>",
Encoding.ASCII.GetString(f.VoicePath))
        Console.WriteLine("CP <{0}>",
Encoding.ASCII.GetString(f.Capability))
        Console.WriteLine("AS {0:X}", f.AccessStatus)
        Console.WriteLine("UL {0:X}", f.UserLoginID)
    End Sub

End Module

Quote:
>-----Original Message-----
>Is there any examples of calling a C++ DLL in vb and

calling the exported
Quote:
>functions?

>I'm not a vb programmer I'm just trying to help one of
our developers out.

>Thanks.

>.



Tue, 24 May 2005 01:58:42 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. VB 5.0 calling C++ 5.0 DLL that calls winsock.dll

2. Compiling a C++ code to build a .DLL using VB Calls Question

3. Pls help: Calling C++ DLL using MFC from VB

4. Calling a C++ DLL function which takes an C++ object as parameter

5. Create DLL from C++ to be used call by VBasic 4.0 application

6. call C++ dll in VB.net

7. Debugging C++ DLL which is called from VB

8. Instability when calling a C++ DLL from VB.NET

9. Calling C++ DLL from VB

10. VB-ActiveX-DLL access violation error when call from C++

11. VB-Error when calling c++ DLL that returns a string

12. From VB calling Dll's written in C++

 

 
Powered by phpBB® Forum Software