Microsoft KB Archive/125794

= PRB: Calling CDatabase::Open in a USRDLL Causes CDBException =

Article ID: 125794

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition</li></ul>

 Microsoft Visual C++ 1.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q125794

<div class="symptoms_section">

SYMPTOMS
Calling the CDatabase::Open function in a USRDLL and using a parameter list of (NULL, FALSE, FALSE, "ODBC;") may generate a CDBException with it's m_strError member set to a value of "Dialog failed."

<div class="cause_section">

CAUSE
When the CDatabase::Open function is called with a value of NULL for the first parameter and the default values for the remaining parameters, an SQL Data Source dialog is displayed to the user allowing them to choose the data source they want to open.

When this is done in the context of a USRDLL, the CDBException is generated because CDatabase::Open attempts to specify the main or active window of the CWinApp or CWinThread object as the parent of the SQL Data Source dialog. In a USRDLL, the main or active window of the CWinApp or CWinThread object is typically NULL.

<div class="resolution_section">

RESOLUTION
Avoid the problem by using the main window of the calling application as the parent of the SQL Data Source dialog. The sample code listed below illustrates one way to implement this solution. The sample DLL exports one function, OpenDatabase, which displays a dialog with a list of data sources. The OpenDatabase function accepts a single parameter of type HWND, which is then used as the handle to the parent window of the SQL Data Source dialog.

<div class="status_section">

STATUS
This behavior was by design, however it was modified in Visual C++ version 2.2. to eliminate the problem.

<div class="moreinformation_section">

Sample Code
/* 16-bit Compile options needed: /GD class MyDLL : public CWinApp { public: BOOL InitInstance; }; extern "C" __declspec(dllexport) int extern "C" int _export FAR PASCAL OpenDatabase(HWND hWndParent /* Needs a parent window */) {   TRY {       // Before calling MyDb.Open setup a parent window AfxGetApp->m_pMainWnd = new CWnd; AfxGetMainWnd->Attach(hWndParent); CDatabase MyDb; // Present list of ODBC sources MyDb.Open(NULL,FALSE,FALSE,"ODBC;"); MyDb.Close; // Clean Up       AfxGetMainWnd->Detach; delete AfxGetMainWnd; AfxGetApp->m_pMainWnd = NULL; }   CATCH_ALL(e) {       if ( AfxGetMainWnd->GetSafeHwnd != NULL ) {           // Clean Up            AfxGetMainWnd->Detach; delete AfxGetMainWnd; AfxGetApp->m_pMainWnd = NULL; }       // Exception caused a failure return FALSE; }   END_CATCH_ALL return TRUE; } BOOL MyDLL::InitInstance {   return TRUE; } MyDLL theDLL;
 * 16-bit Link options needed:   /LIB:"odbc"
 * 1) ifndef _WINDLL       // Make this work with WIN32 and WIN16
 * 2) define _WINDLL
 * 3) endif
 * 4) ifndef _USRDLL
 * 5) define _USRDLL
 * 6) endif
 * 7) include <afxwin.h>
 * 8) include <afxext.h>
 * 9) include <afxdb.h>
 * 1) include <afxdb.h>
 * 1) ifdef WIN32   // Make this work with WIN32 and WIN16
 * 1) else
 * 1) endif

Additional query words: 1.00 1.50 2.00 2.10 2.50 2.51 2.52 3.00 3.10

Keywords: kbprb kbcode KB125794

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.