Error -2147467259 connecting to a database from VB6
I'm currently working on a small application that needs to connect to a database. I do this using the following code...
Set database_connection = New ADODB.Connection
database_connection.ConnectionString = _
"Driver={MySQL ODBC 3.51 Driver}; Server=HOST; " & _
"Database=SCHEMA; " & _
"User=USER; " & _
"Password=PASSWORD; " & _
"Option=3;"
database_connection.Open
This works fine when I run the project from the IDE, and it works file when I run the compiled exe from the command line. If I attempt to run the exe by invoking the CreateProcess function, though, it doesn't work at all, producing the following error message...
(1) Error#: -2147467259
Desc. : Unspecified error
Source: Provider
Native Error: -2147467259
SQL State:
Help Context: 1240640
Help File:
Does anyone know what I ought to do about this? I'm working on开发者_开发技巧 Windows XP, and the CreateProcess call looks like...
Dim create_result As Long
Dim startup_information As STARTUPINFO
Dim our_process_information As PROCESS_INFORMATION
Dim process_attributes As SECURITY_ATTRIBUTES
Dim thread_attributes As SECURITY_ATTRIBUTES
create_result = CreateProcess(vbNullString, _
command_line, _
process_attributes, _
thread_attributes, _
0, _
0, _
0, _
vbNullString, _
startup_information, _
our_process_information)
(I suspect a permissions problem but don't know what to do about it if it is. Nothing is appearing in the server log.)
Hmm, that coding style looks awfully familiar...
One mistake people commonly make is using the "stock" Declare
signatures from sources such as API Viewer without understanding what they mean, e.g.:
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" ( _
ByVal lpApplicationName As String, _
ByVal lpCommandLine As String, _
lpProcessAttributes As SECURITY_ATTRIBUTES, _
lpThreadAttributes As SECURITY_ATTRIBUTES, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
lpEnvironment As Any, _
ByVal lpCurrentDriectory As String, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long
Those As String
and As Any
declarations have impacts that people often ignore.
Generally one is far, far better off using the "W" entrypoints and declaring all pointers as ByVal xxx As Long
, then applying the VB6 pointer functions where required. But you can lead a horse to water...
Try this:
create_result = CreateProcess(vbNullString, _
command_line, _
process_attributes, _
thread_attributes, _
0, _
0, _
ByVal 0&, _
vbNullString, _
startup_information, _
our_process_information)
Or far, far better, try:
Private Declare Function CreateProcessW Lib "kernel32" ( _
ByVal lpApplicationName As Long, _
ByVal lpCommandLine As Long, _
ByVal lpProcessAttributes As Long, _
ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, _
ByVal lpCurrentDriectory As Long, _
ByVal lpStartupInfo As Long, _
ByVal lpProcessInformation As Long) As Long
Dim create_result As Long
Dim startup_information As STARTUPINFO
Dim our_process_information As PROCESS_INFORMATION
create_result = CreateProcessW(0, _
StrPtr(command_line), _
0, _
0, _
0, _
0, _
0, _
0, _
VarPtr(startup_information), _
VarPtr(our_process_information))
My guess is that this is exactly where you were "breaking" your ODBC driver (sadly an OLEDB Provider would be much better, but I don't know of a decent free one for MySQL).
Oh, the problem (and difference here)?
You were zapping the environment block to nothing, i.e. probably breaking PATH keeps your ODBC driver from working.
Null pointers, zeros, and empty structures are entirely different things.
精彩评论