Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have recently updated my vb.net winform desktop application from framework 4.0 to 4.6.1.
I have been using odbc32.dll for my sql server related query.

It is no longer working in 4.6.1.
It failed in SQLAllocHandle(SQL_HANDLE_DBC, hEnv, hDbc)
The error : System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

It is only error in 64 bit, it works fine in 32 bit.

Please share your wisdom to me, thank you so much

What I have tried:

#Region "DLL Imports"
    Private Declare Function SQLAllocEnv Lib "odbc32.dll" (ByRef phenv As Integer) As Integer
    Private Declare Function SQLAllocHandle Lib "odbc32.dll" (ByVal hType As Integer, ByVal hInput As Integer, ByRef phOutput As Integer) As Integer
    Private Declare Function SQLBrowseConnect Lib "odbc32.dll" (ByVal hDbc As Integer, ByVal szConnStrIn As String, ByVal cbConnStrIn As Integer, ByVal szConnStrOut As String, ByVal cbConnStrOutMax As Integer, ByRef pcbconnstrout As Integer) As Integer
    Private Declare Function SQLDisconnect Lib "odbc32.dll" (ByVal hDbc As Integer) As Integer
    Private Declare Function SQLFreeConnect Lib "odbc32.dll" (ByVal hDbc As Integer) As Integer
    Private Declare Function SQLFreeEnv Lib "odbc32.dll" (ByVal hEnv As Integer) As Integer
    Private Const SQL_HANDLE_DBC = 2
#End Region

  Public Function GetSQLServers() As String

        Dim retCode As Integer
        Dim hDbc As Integer
        Dim hEnv As Integer
        Dim strCon As String
        Dim strOutCon As String
        Dim intConLenOut As Integer

        strCon = "DRIVER={SQL Server};"
        strOutCon = Space(1000)

        retCode = SQLAllocEnv(hEnv)
        retCode = SQLAllocHandle(SQL_HANDLE_DBC, hEnv, hDbc)
        retCode = SQLBrowseConnect(hDbc, strCon, Len(strCon), strOutCon, Len(strOutCon) + 2, intConLenOut)

    End Function
Posted
Updated 28-Jul-20 22:33pm

1 solution

You cannot mix 32 and 64 bit components in the same application.
You will need to either find a 64bit version of you DLL, or compile you app to 32 bits to use it.
 
Share this answer
 
Comments
ekograce 29-Jul-20 20:43pm    
thank you :)
OriginalGriff 30-Jul-20 2:20am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900