begin process at 2008 07 05 07:38:17
1 205 059 membres
45 nouveaux aujourd'hui
14 118 membres club

Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

AFFICHER L'INTERFACE ODBC POUR CRÉER UNE SOURCE DE DONNÉE (DSN)


Information sur la source

Catégorie :Base de Donnees Classé sous : dsn, odbc, interface Niveau : Initié Date de création : 29/07/2005 Date de mise à jour : 05/08/2005 16:18:45 Vu : 4 311

Note :
8 / 10 - par 1 personne
8,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

Commentaire sur cette source (10)
Ajouter un commentaire et/ou une note

Description

Permet de créer un DSN en affichant l'interface "Sources de données ODBC" des outils d'administration.

Note : l'API "CreateProcess" est préférable à la fonction "Shell".

Source

  • Option Explicit
  • Private Enum udeSW
  • SW_HIDE = 0
  • SW_NORMAL = 1
  • SW_MAXIMIZE = 3
  • SW_MINIMIZE = 6
  • End Enum
  • Private Type PROCESS_INFORMATION
  • hProcess As Long
  • hThread As Long
  • dwProcessId As Long
  • dwThreadId As Long
  • End Type
  • Private Type STARTUPINFO
  • cb As Long
  • lpReserved As String
  • lpDesktop As String
  • lpTitle As String
  • dwX As Long
  • dwY As Long
  • dwXSize As Long
  • dwYSize As Long
  • dwXCountChars As Long
  • dwYCountChars As Long
  • dwFillAttribute As Long
  • dwFlags As Long
  • wShowWindow As Integer
  • cbReserved2 As Integer
  • lpReserved2 As Byte
  • hStdInput As Long
  • hStdOutput As Long
  • hStdError As Long
  • End Type
  • Private Type SECURITY_ATTRIBUTES
  • nLength As Long
  • lpSecurityDescriptor As Long
  • bInheritHandle As Long
  • End Type
  • Private Enum udePriority_Class
  • NORMAL_PRIORITY_CLASS = &H20
  • IDLE_PRIORITY_CLASS = &H40
  • HIGH_PRIORITY_CLASS = &H80
  • End Enum
  • 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
  • Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
  • Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
  • Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  • Private Declare Function GetActiveWindow Lib "user32" () As Long
  • Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
  • Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
  • Sub DisplayODBCManager()
  • Dim SysDir As String, Ret As Long
  • SysDir = Space(255)
  • Ret = GetSystemDirectory(SysDir, 255)
  • SysDir = Left$(SysDir, Ret) & "\"
  • 'Retourne zéro en cas d'erreur
  • If NewProcess(SysDir & "rundll32.exe", "shell32.dll,Control_RunDLL odbccp32.cpl", SW_NORMAL, IDLE_PRIORITY_CLASS) Then
  • 'Le formulaire ODBC a été ouvert et fermé. Après vérification en base de registre
  • 'de son existence, le DSN peut être utilisé pour tenter d'ouvrir une connexion.
  • Else
  • 'Echec de l'ouverture du formulaire ODBC
  • End If
  • End Sub
  • Private Function NewProcess(AppPath As String, Arg As String, ByVal SW_Stat As udeSW, ByVal Priority_Class As udePriority_Class) As Long
  • Dim PClass As Long
  • Dim CmdLine As String
  • Dim SInfo As STARTUPINFO
  • Dim PInfo As PROCESS_INFORMATION
  • Dim Sec1 As SECURITY_ATTRIBUTES
  • Dim Sec2 As SECURITY_ATTRIBUTES
  • Dim hDC As Long
  • Const HORZRES = 8
  • Const VERTRES = 10
  • Const INFINITE = &HFFFF
  • Const WAIT_TIMEOUT As Long = 258&
  • Const STARTF_USESHOWWINDOW = &H1
  • Const STARTF_USEPOSITION = &H4
  • Sec1.nLength = Len(Sec1)
  • Sec2.nLength = Len(Sec2)
  • hDC = GetDC(GetActiveWindow())
  • With SInfo
  • .cb = Len(SInfo)
  • .dwX = GetDeviceCaps(hDC, HORZRES) \ 2
  • .dwY = GetDeviceCaps(hDC, VERTRES) \ 2
  • .dwFlags = STARTF_USEPOSITION Or STARTF_USESHOWWINDOW
  • .wShowWindow = SW_Stat
  • End With
  • PClass = Priority_Class
  • CmdLine = AppPath
  • If (Len(Trim$(Arg)) > 0) Then
  • CmdLine = CmdLine & " " & Arg
  • End If
  • NewProcess = CreateProcess(AppPath, CmdLine, Sec1, Sec2, False, PClass, 0&, CurDir$(), SInfo, PInfo)
  • If NewProcess Then
  • 'Sans rafraîchissement du formulaire parent
  • 'WaitForSingleObject pinfo.hProcess, INFINITE
  • 'Avec rafraîchissement du formulaire parent
  • Do While WaitForSingleObject(PInfo.hProcess, 10) = WAIT_TIMEOUT
  • DoEvents
  • Loop
  • Call CloseHandle(PInfo.hProcess)
  • Call CloseHandle(PInfo.hThread)
  • End If
  • End Function
Option Explicit

Private Enum udeSW
    SW_HIDE = 0
    SW_NORMAL = 1
    SW_MAXIMIZE = 3
    SW_MINIMIZE = 6
End Enum

Private Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessId As Long
    dwThreadId As Long
End Type

Private Type STARTUPINFO
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Byte
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type

Private Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
End Type

Private Enum udePriority_Class
    NORMAL_PRIORITY_CLASS = &H20
    IDLE_PRIORITY_CLASS = &H40
    HIGH_PRIORITY_CLASS = &H80
End Enum

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
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long

Sub DisplayODBCManager()
   Dim SysDir As String, Ret As Long
   
    SysDir = Space(255)
    Ret = GetSystemDirectory(SysDir, 255)
    SysDir = Left$(SysDir, Ret) & "\"
    
   'Retourne zéro en cas d'erreur
   If NewProcess(SysDir & "rundll32.exe", "shell32.dll,Control_RunDLL odbccp32.cpl", SW_NORMAL, IDLE_PRIORITY_CLASS) Then
      'Le formulaire ODBC a été ouvert et fermé. Après vérification en base de registre
      'de son existence, le DSN peut être utilisé pour tenter d'ouvrir une connexion.
   Else
      'Echec de l'ouverture du formulaire ODBC
   End If
   
End Sub

Private Function NewProcess(AppPath As String, Arg As String, ByVal SW_Stat As udeSW, ByVal Priority_Class As udePriority_Class) As Long

    Dim PClass As Long
    Dim CmdLine As String
    Dim SInfo As STARTUPINFO
    Dim PInfo As PROCESS_INFORMATION
    Dim Sec1 As SECURITY_ATTRIBUTES
    Dim Sec2 As SECURITY_ATTRIBUTES
    Dim hDC As Long
    
    Const HORZRES = 8
    Const VERTRES = 10
    Const INFINITE = &HFFFF
    Const WAIT_TIMEOUT As Long = 258&
    Const STARTF_USESHOWWINDOW = &H1
    Const STARTF_USEPOSITION = &H4

    Sec1.nLength = Len(Sec1)
    Sec2.nLength = Len(Sec2)
    hDC = GetDC(GetActiveWindow())
    With SInfo
      .cb = Len(SInfo)
      .dwX = GetDeviceCaps(hDC, HORZRES) \ 2
      .dwY = GetDeviceCaps(hDC, VERTRES) \ 2
      .dwFlags = STARTF_USEPOSITION Or STARTF_USESHOWWINDOW
      .wShowWindow = SW_Stat
    End With
    PClass = Priority_Class
     
    CmdLine = AppPath
    If (Len(Trim$(Arg)) > 0) Then
        CmdLine = CmdLine & " " & Arg
    End If
   
   NewProcess = CreateProcess(AppPath, CmdLine, Sec1, Sec2, False, PClass, 0&, CurDir$(), SInfo, PInfo)
   If NewProcess Then
      'Sans rafraîchissement du formulaire parent
      'WaitForSingleObject pinfo.hProcess, INFINITE
      
      'Avec rafraîchissement du formulaire parent
      Do While WaitForSingleObject(PInfo.hProcess, 10) = WAIT_TIMEOUT
           DoEvents
       Loop
   
      Call CloseHandle(PInfo.hProcess)
      Call CloseHandle(PInfo.hThread)
   End If
End Function

Conclusion

Bibliographie :
http://vbfrance.com/code.aspx?ID=6839
http://windowsxp.mvps.org/rundll32.htm
02 août 2005 14:35:12 :
Ajout des instructions de fermeture
03 août 2005 17:39:12 :
Code exemple avec / sans rafraîchissement de la fenêtre parent
03 août 2005 18:02:02 :
MAJ du code exemple
04 août 2005 16:31:51 :
ajout pour centrer l'affichage
04 août 2005 16:41:54 :
MAJ
05 août 2005 16:18:45 :
Ajout d'une capture d'écran
  • signaler à un administrateur
    Commentaire de FENETRES le 29/07/2005 15:39:08



    Autre lien intéressant :
    http://www.vbexplorer.com/VBExplorer/shellcpl.htm

  • signaler à un administrateur
    Commentaire de mythic_kruger le 02/08/2005 06:23:59

    Question: en quoi CreateProcess est préférable à la fonction "Shell"?

  • signaler à un administrateur
    Commentaire de FENETRES le 02/08/2005 09:40:15

    Au niveau des possibilités du paramétrage entrant et sortant
    (traitement multitâches par exemple).
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createprocess.asp)

  • signaler à un administrateur
    Commentaire de FENETRES le 02/08/2005 09:54:58


    D'ailleurs, j'aurais dû ajouter après l'appel à "WaitForSingleObject" les instructions suivantes de fermeture :
        call CloseHandle(pinfo.hProcess);
        call CloseHandle(pinfo.hThread);

  • signaler à un administrateur
    Commentaire de FENETRES le 02/08/2005 10:12:56


    L'adresse ci-dessous propose un exemple d'utilisation de la fonction shell.
    http://docvb.free.fr/apidetail.php?idapi=139

  • signaler à un administrateur
    Commentaire de FENETRES le 02/08/2005 10:39:25


    Plus trivialement, pour cet exemple, il faut attendre la fermeture du formulaire ODBC pour utiliser le DSN.

  • signaler à un administrateur
    Commentaire de FENETRES le 03/08/2005 17:29:51

    Nota bene : pour rafraîchir votre propre formulaire, il faut remplacer l'instruction
    --------------------------------------------------------
       WaitForSingleObject pinfo.hProcess, Timeout
    --------------------------------------------------------
       par
    --------------------------------------------------------
       Do While WaitForSingleObject(pinfo.hProcess, 10) = WAIT_TIMEOUT
            DoEvents
        Loop
    --------------------------------------------------------

  • signaler à un administrateur
    Commentaire de FENETRES le 05/08/2005 10:35:58


    MSDN : Multi-threading et synchronisation
    http://www.microsoft.com/belux/fr/msdn/community/columns/ldoc/multithread1.mspx

  • signaler à un administrateur
    Commentaire de djina le 17/08/2005 12:56:50

    Je Veux débuter en VB.net. Que me conseillez-vous?

  • signaler à un administrateur
    Commentaire de FENETRES le 19/09/2006 10:39:08

    Pour les débutants, une technique plus simple consiste à créer et à ouvrir un fichier portant l'extension udl.
    Une fois la source de données ODBC créée, il suffit de lire le fichier texte pour récupérer la chaîne de connexion.

Ajouter un commentaire

Discussions en rapport avec ce code source

Pub



Appels d'offres

CalendriCode

Juillet 2008
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

VS Express FR Gratuit !

VS Express en français et 100% gratuit !

Boutique

Boutique de goodies CodeS-SourceS