Instability when calling a C++ DLL from VB.NET 
Author Message
 Instability when calling a C++ DLL from VB.NET

Hello all. I apologize in advance for the relatively vague description of my
problem, but here goes:

I have several C++ DLLs (some that I wrote, some black boxes that I have
from vendors), and I am calling their exposed subroutines from VB.NET. I
have declared all of the exposed subroutines as normal (occasionally, I do
use the shortcuts of $, #, &, etc. for argument declarations), and I have
been calling the subroutines.

In Visual Basic 6.0, everything works fine. However, I seem to be having
problems in VB.NET. Seemingly randomly, my code will throw an object null
reference exception when calling the subroutines. The only argument to most
of the subroutines is an integer, and the de{*filter*} tells me that the integer
does in fact exist and has a valid value. It fails only on calls to these
external DLLs, but it fails on different subroutines each time.

Any thoughts? One of the external DLLs is a VC++ 6.0 DLL, so perhaps that is
the problem? Unfortunately, that one's not mine, so any workarounds would be
appreciated. I tried wrapping all the calls in Try/Catches for the Exception
and just forcing them to try again. That works most of the time, but
occasionally, the damn thing gets too stubborn and falls into an infinite
loop.

Hopefully someone out there has at least experienced this behavior as well?
I have only found one veiled reference to something similar in Google, but
no one answered the poor guy.

Thanks in advance for any thoughts you might have!

-- Venk --



Thu, 27 Jan 2005 03:28:17 GMT  
 Instability when calling a C++ DLL from VB.NET

Are you sure you have declared the parameter types correctly? Keep in
mind that the size of the integer types are different in VB6 and
VB.NET.

Also, make sure you are using the right calling convention (stdcall,
cdecl etc.).

Mattias

===
Mattias Sj?gren (VB MVP)

http://www.msjogren.net/dotnet/



Thu, 27 Jan 2005 05:38:19 GMT  
 Instability when calling a C++ DLL from VB.NET
Yeah, already checked those.

The functions actually work sometimes but not others. The
NullReferenceException appears to be quite random.


Quote:

> Are you sure you have declared the parameter types correctly? Keep in
> mind that the size of the integer types are different in VB6 and
> VB.NET.

> Also, make sure you are using the right calling convention (stdcall,
> cdecl etc.).

> Mattias

> ===
> Mattias Sj?gren (VB MVP)

> http://www.msjogren.net/dotnet/



Thu, 27 Jan 2005 06:23:02 GMT  
 Instability when calling a C++ DLL from VB.NET
I call C++ DLLs (unmanaged) all the time.  On Monday I
will post some sample code here for you.  Code is a work
and I am at home

Pat

Quote:
>-----Original Message-----
>Yeah, already checked those.

>The functions actually work sometimes but not others. The
>NullReferenceException appears to be quite random.




>> Are you sure you have declared the parameter types
correctly? Keep in
>> mind that the size of the integer types are different
in VB6 and
>> VB.NET.

>> Also, make sure you are using the right calling

convention (stdcall,

- Show quoted text -

Quote:
>> cdecl etc.).

>> Mattias

>> ===
>> Mattias Sj?gren (VB MVP)

>> http://www.msjogren.net/dotnet/

>.



Fri, 28 Jan 2005 08:40:54 GMT  
 Instability when calling a C++ DLL from VB.NET
*****************************************
*                                       *
*   TestUDTPassing VB.NET source code   *
*                                       *
*****************************************

Imports System.Text
Imports System.Runtime.InteropServices
Imports System.ArgumentException

Module TestUDTPassing

    Sub Main()
        Dim intResults As Integer
        Dim TestPassing As New clsTestPassing()

        ' Load some data
        TestPassing.intAccessStatus = &H11223344
        TestPassing.strUserName = "patrick"
        intResults = TestPassing.ReadTestPassing()
        Console.WriteLine("Access Status = " & Hex
(TestPassing.intAccessStatus))
        Console.WriteLine("User Name <" &
TestPassing.strUserName & ">")
    End Sub

End Module

<StructLayout(LayoutKind.Sequential)> Public Structure
DATA_TestPassing
    Public AccessStatus As Integer
    <MarshalAs(UnmanagedType.ByValArray, SizeConst:=20)>
Public UserName() As Byte
End Structure

Public Class clsTestPassing
    Dim DataEncode As New System.Text.ASCIIEncoding()
    Dim m_strUserName As String
    Dim REC_TestPassing As New DATA_TestPassing()

    Private Const SIZE_TestPassing_UserName As Integer = 16
    Private Const ASIS_TestPassing_UserName As Integer = 20

    Public Sub New()
        ReDim REC_TestPassing.UserName(20)
        Console.WriteLine("New invoked")
    End Sub

    Public Property intAccessStatus() As Integer

        Get
            Return REC_TestPassing.AccessStatus
        End Get

        Set(ByVal Value As Integer)
            REC_TestPassing.AccessStatus = Value
        End Set

    End Property

    Public Property strUserName() As String

        Get
            Return m_strUserName
        End Get

        Set(ByVal Value As String)
            If (Value.Length > SIZE_TestPassing_UserName)
Then
                m_strUserName = Value.Substring(0,
SIZE_TestPassing_UserName)
            Else
                m_strUserName = Value
            End If
        End Set

    End Property

    Private Sub UnloadTestPassing()
        Dim intPosition As Integer

        m_strUserName = DataEncode.GetString
(REC_TestPassing.UserName)
        intPosition = m_strUserName.IndexOf(Chr(0))
        If intPosition = 0 Then
            m_strUserName = ""
        Else
            m_strUserName = m_strUserName.Substring(0,
intPosition)
        End If
    End Sub

    Private Sub LoadTestPassing()
        Encoding.ASCII.GetBytes(m_strUserName.ToCharArray,
0, m_strUserName.Length, REC_TestPassing.UserName, 0)
        REC_TestPassing.UserName(m_strUserName.Length) = 0
    End Sub

    Private Declare Function AccessTestPassing _
          Lib "c:\Pat\VS6\VC6
\TestUDTPassing\debug\TestUDTPassing" _
            (ByVal intAccessType As Integer, _
             ByRef REC_TestPassing As DATA_TestPassing) _
        As Integer

    Public Function ReadTestPassing() As Integer
        Dim intAccessType As Integer
        Dim intResults As Integer

        LoadTestPassing()
        intAccessType = &HAABBCCDD
        intResults = AccessTestPassing(intAccessType, _
                                       REC_TestPassing)
        UnloadTestPassing()
    End Function

End Class

******************************************
*                                        *
*   TestUDTPassing DLL DEF source code   *
*                                        *
******************************************

LIBRARY TestUDTPassing

EXPORTS

******************************************
*                                        *
*   TestUDTPassing DLL CPP source code   *
*                                        *
******************************************

/*

  TestUDTPassing.cpp

  This module is the DLL that interfaces VB.NET/C++
  to test passing a UDT with a string

  Modification History

     DATE     PROGRAMMER             Modification
  ----------  ---------------------  ----------------------
-----------
  2002/05/06  Patrick Ireland        Original
  2002/08/12  Patrick Ireland        Update documentation

*/

/**********************************************************
**********
*                                                          
         *
*  System
includes                                                  *
*                                                          
         *
***********************************************************
*********/

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

/**********************************************************
**********
*                                                          
         *
*  Program
definitions                                              *
*                                                          
         *
***********************************************************
*********/

#define MODULE_NAME            "TestUDTPassing"
#define MODULE_SPACE           "              "
#define MODULE_COPYRIGHT       "(C) Copyright 2002 "
#define MODULE_OWNER           "by Patrick Wayne Ireland "
#define MODULE_AUTHOR          "Patrick Wayne Ireland "
#define MODULE_RIGHTS          "All Rights Reserved "
#define MODULE_VERSION         "2000/08/14 21:30 "

/**********************************************************
**********
*                                                          
         *
*  Structure
definitions                                            *
*                                                          
         *
***********************************************************
*********/

typedef struct
        {
        int                        intAccessStatus;
        char                       szUserName [ 20 ];
        }   TestPassing;

/**********************************************************
**********
*                                                          
         *
*  Function
Prototypes                                              *
*                                                          
         *
***********************************************************
*********/

static void HexDump

    (char                    * pszHeaderMessage,
     BYTE                    * pabBytesToDump,
     long                      lBytesToDump);

/**********************************************************
**********
*                                                          
         *
*  AccessTestPassing
()                                             *
*                                                          
         *
***********************************************************
*********/

DWORD __stdcall AccessTestPassing

    (int                       intAccessType,
         TestPassing             * pTestPassing)

{
printf ("sizeof TestPassing %d\n",
            sizeof (TestPassing));
HexDump ("AccessStatus",
             (BYTE *) &intAccessType,
                 sizeof (int));
HexDump ("Record",
             (BYTE *) pTestPassing,
                 sizeof (TestPassing));
strcpy ((char *) pTestPassing->szUserName,
            "HELLO WORLD");
pTestPassing->intAccessStatus = 0x12345678;
HexDump ("Out Record",
             (BYTE *) pTestPassing,
                 sizeof (TestPassing));
return 0;

Quote:
}   // DWORD __stdcall AccessTestPassing

//
// extern "C" __declspec(dllexport) DWORD __stdcall
AccessTestPassing

/**********************************************************
**********
*                                                          
         *
*  Hexdump Support
Data                                             *
*                                                          
         *
***********************************************************
*********/

static char                    achPrintableChars [256] =
      { '.', '.', '.', '.', '.', '.', '.', '.',
        '.', '.', '.', '.', '.', '.', '.', '.',
        '.', '.', '.', '.', '.', '.', '.', '.',
        '.', '.', '.', '.', '.', '.', '.', '.',
        ' ', '!', '"', '#', '$', '%', '&', '\'',
        '(', ')', '*', '+', ',', '-', '.', '/',
        '0', '1', '2', '3', '4', '5', '6', '7',
        '8', '9', ':', ';', '<', '=', '>', '?',

        'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
        'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
        'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
        '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
        'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
        'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
        'x', 'y', 'z', '{', '|', '}', '~', '.',
        '.', '.', '.', '.', '.', '.', '.', '.',
        '.', '.', '.', '.', '.', '.', '.', '.',
        '.', '.', '.', '.', '.', '.', '.', '.',
        '.', '.', '.', '.', '.', '.', '.', '.',
        '.', '.', '.', '.', '.', '.', '.', '.',
        '.', '.', '.', '.', '.', '.', '.', '.',
        '.', '.', '.', '.', '.', '.', '.', '.',
        '.', '.', '.', '.', '.', '.', '.', '.',
        '.', '.', '.', '.', '.', '.', '.', '.',
        '.', '.', '.', '.', '.', '.', '.', '.',
        '.', '.', '.', '.', '.', '.', '.', '.',
        '.', '.', '.', '.', '.', '.', '.', '.',
        '.', '.', '.', '.', '.', '.', '.', '.',
        '.', '.', '.', '.', '.', '.', '.', '.',
        '.', '.', '.', '.', '.', '.', '.', '.',
        '.', '.', '.', '.', '.', '.', '.', '.' };

static char                    achHexAsAscii [16] =
      { '0', '1', '2', '3', '4', '5', '6', '7',
        '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

/**********************************************************
**********
*                                                          
         *
*  HexDump
()                                                       *
*                                                          
         *
***********************************************************
*********/

static void HexDump

    (char                    * pszHeaderMessage,
     BYTE                    * pabBytesToDump,
     long                      lBytesToDump)

{
char                       szOutputLine [51];
long                       lOutputIndex;
long                       lByteCount;
long                      
...

read more »



Fri, 28 Jan 2005 23:00:27 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. call C++ dll in VB.net

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

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

4. Problem using SmtpMail.Send() in a VB.NET dll with a C++ string

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

6. Calling a C++ DLL using vb

7. Debug VB.NET app and C++ DLL

8. Calling C++ DLL from VB

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

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

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

12. VB DLL's called from C++

 

 
Powered by phpBB® Forum Software