Win32 API 日本語リファレンス
ホームSystem.Search › SQLConnect

SQLConnect

関数
DSN名とユーザー認証でデータソースに接続する。
DLLODBC32.dll文字セットANSI (-A)呼出規約winapi

シグネチャ

// ODBC32.dll  (ANSI / -A)
#include <windows.h>

SHORT SQLConnect(
    void* ConnectionHandle,
    BYTE* ServerName,
    SHORT NameLength1,
    BYTE* UserName,
    SHORT NameLength2,
    BYTE* Authentication,
    SHORT NameLength3
);

パラメーター

名前方向説明
ConnectionHandlevoid*inout接続に使用する接続ハンドル(HDBC)。
ServerNameBYTE*in接続先データソース名(DSN)。
NameLength1SHORTinServerNameの長さ。SQL_NTSで終端まで。
UserNameBYTE*in認証に使うユーザー名。
NameLength2SHORTinUserNameの長さ。SQL_NTSで終端まで。
AuthenticationBYTE*in認証に使うパスワード。
NameLength3SHORTinAuthenticationの長さ。SQL_NTSで終端まで。

戻り値の型: SHORT

各言語での呼び出し定義

// ODBC32.dll  (ANSI / -A)
#include <windows.h>

SHORT SQLConnect(
    void* ConnectionHandle,
    BYTE* ServerName,
    SHORT NameLength1,
    BYTE* UserName,
    SHORT NameLength2,
    BYTE* Authentication,
    SHORT NameLength3
);
[DllImport("ODBC32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern short SQLConnect(
    IntPtr ConnectionHandle,   // void* in/out
    IntPtr ServerName,   // BYTE*
    short NameLength1,   // SHORT
    IntPtr UserName,   // BYTE*
    short NameLength2,   // SHORT
    IntPtr Authentication,   // BYTE*
    short NameLength3   // SHORT
);
<DllImport("ODBC32.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function SQLConnect(
    ConnectionHandle As IntPtr,   ' void* in/out
    ServerName As IntPtr,   ' BYTE*
    NameLength1 As Short,   ' SHORT
    UserName As IntPtr,   ' BYTE*
    NameLength2 As Short,   ' SHORT
    Authentication As IntPtr,   ' BYTE*
    NameLength3 As Short   ' SHORT
) As Short
End Function
' ConnectionHandle : void* in/out
' ServerName : BYTE*
' NameLength1 : SHORT
' UserName : BYTE*
' NameLength2 : SHORT
' Authentication : BYTE*
' NameLength3 : SHORT
Declare PtrSafe Function SQLConnect Lib "odbc32" ( _
    ByVal ConnectionHandle As LongPtr, _
    ByVal ServerName As LongPtr, _
    ByVal NameLength1 As Integer, _
    ByVal UserName As LongPtr, _
    ByVal NameLength2 As Integer, _
    ByVal Authentication As LongPtr, _
    ByVal NameLength3 As Integer) As Integer
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SQLConnect = ctypes.windll.odbc32.SQLConnect
SQLConnect.restype = ctypes.c_short
SQLConnect.argtypes = [
    ctypes.POINTER(None),  # ConnectionHandle : void* in/out
    ctypes.POINTER(ctypes.c_ubyte),  # ServerName : BYTE*
    ctypes.c_short,  # NameLength1 : SHORT
    ctypes.POINTER(ctypes.c_ubyte),  # UserName : BYTE*
    ctypes.c_short,  # NameLength2 : SHORT
    ctypes.POINTER(ctypes.c_ubyte),  # Authentication : BYTE*
    ctypes.c_short,  # NameLength3 : SHORT
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ODBC32.dll')
SQLConnect = Fiddle::Function.new(
  lib['SQLConnect'],
  [
    Fiddle::TYPE_VOIDP,  # ConnectionHandle : void* in/out
    Fiddle::TYPE_VOIDP,  # ServerName : BYTE*
    Fiddle::TYPE_SHORT,  # NameLength1 : SHORT
    Fiddle::TYPE_VOIDP,  # UserName : BYTE*
    Fiddle::TYPE_SHORT,  # NameLength2 : SHORT
    Fiddle::TYPE_VOIDP,  # Authentication : BYTE*
    Fiddle::TYPE_SHORT,  # NameLength3 : SHORT
  ],
  Fiddle::TYPE_SHORT)
#[link(name = "odbc32")]
extern "system" {
    fn SQLConnect(
        ConnectionHandle: *mut (),  // void* in/out
        ServerName: *mut u8,  // BYTE*
        NameLength1: i16,  // SHORT
        UserName: *mut u8,  // BYTE*
        NameLength2: i16,  // SHORT
        Authentication: *mut u8,  // BYTE*
        NameLength3: i16  // SHORT
    ) -> i16;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("ODBC32.dll", CharSet = CharSet.Ansi)]
public static extern short SQLConnect(IntPtr ConnectionHandle, IntPtr ServerName, short NameLength1, IntPtr UserName, short NameLength2, IntPtr Authentication, short NameLength3);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ODBC32_SQLConnect' -Namespace Win32 -PassThru
# $api::SQLConnect(ConnectionHandle, ServerName, NameLength1, UserName, NameLength2, Authentication, NameLength3)
#uselib "ODBC32.dll"
#func global SQLConnect "SQLConnect" sptr, sptr, sptr, sptr, sptr, sptr, sptr
; SQLConnect ConnectionHandle, varptr(ServerName), NameLength1, varptr(UserName), NameLength2, varptr(Authentication), NameLength3   ; 戻り値は stat
; ConnectionHandle : void* in/out -> "sptr"
; ServerName : BYTE* -> "sptr"
; NameLength1 : SHORT -> "sptr"
; UserName : BYTE* -> "sptr"
; NameLength2 : SHORT -> "sptr"
; Authentication : BYTE* -> "sptr"
; NameLength3 : SHORT -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ODBC32.dll"
#cfunc global SQLConnect "SQLConnect" sptr, var, int, var, int, var, int
; res = SQLConnect(ConnectionHandle, ServerName, NameLength1, UserName, NameLength2, Authentication, NameLength3)
; ConnectionHandle : void* in/out -> "sptr"
; ServerName : BYTE* -> "var"
; NameLength1 : SHORT -> "int"
; UserName : BYTE* -> "var"
; NameLength2 : SHORT -> "int"
; Authentication : BYTE* -> "var"
; NameLength3 : SHORT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; SHORT SQLConnect(void* ConnectionHandle, BYTE* ServerName, SHORT NameLength1, BYTE* UserName, SHORT NameLength2, BYTE* Authentication, SHORT NameLength3)
#uselib "ODBC32.dll"
#cfunc global SQLConnect "SQLConnect" intptr, var, int, var, int, var, int
; res = SQLConnect(ConnectionHandle, ServerName, NameLength1, UserName, NameLength2, Authentication, NameLength3)
; ConnectionHandle : void* in/out -> "intptr"
; ServerName : BYTE* -> "var"
; NameLength1 : SHORT -> "int"
; UserName : BYTE* -> "var"
; NameLength2 : SHORT -> "int"
; Authentication : BYTE* -> "var"
; NameLength3 : SHORT -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	odbc32 = windows.NewLazySystemDLL("ODBC32.dll")
	procSQLConnect = odbc32.NewProc("SQLConnect")
)

// ConnectionHandle (void* in/out), ServerName (BYTE*), NameLength1 (SHORT), UserName (BYTE*), NameLength2 (SHORT), Authentication (BYTE*), NameLength3 (SHORT)
r1, _, err := procSQLConnect.Call(
	uintptr(ConnectionHandle),
	uintptr(ServerName),
	uintptr(NameLength1),
	uintptr(UserName),
	uintptr(NameLength2),
	uintptr(Authentication),
	uintptr(NameLength3),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // SHORT
function SQLConnect(
  ConnectionHandle: Pointer;   // void* in/out
  ServerName: Pointer;   // BYTE*
  NameLength1: Smallint;   // SHORT
  UserName: Pointer;   // BYTE*
  NameLength2: Smallint;   // SHORT
  Authentication: Pointer;   // BYTE*
  NameLength3: Smallint   // SHORT
): Smallint; stdcall;
  external 'ODBC32.dll' name 'SQLConnect';
result := DllCall("ODBC32\SQLConnect"
    , "Ptr", ConnectionHandle   ; void* in/out
    , "Ptr", ServerName   ; BYTE*
    , "Short", NameLength1   ; SHORT
    , "Ptr", UserName   ; BYTE*
    , "Short", NameLength2   ; SHORT
    , "Ptr", Authentication   ; BYTE*
    , "Short", NameLength3   ; SHORT
    , "Short")   ; return: SHORT
●SQLConnect(ConnectionHandle, ServerName, NameLength1, UserName, NameLength2, Authentication, NameLength3) = DLL("ODBC32.dll", "int SQLConnect(void*, void*, int, void*, int, void*, int)")
# 呼び出し: SQLConnect(ConnectionHandle, ServerName, NameLength1, UserName, NameLength2, Authentication, NameLength3)
# ConnectionHandle : void* in/out -> "void*"
# ServerName : BYTE* -> "void*"
# NameLength1 : SHORT -> "int"
# UserName : BYTE* -> "void*"
# NameLength2 : SHORT -> "int"
# Authentication : BYTE* -> "void*"
# NameLength3 : SHORT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "odbc32" fn SQLConnect(
    ConnectionHandle: ?*anyopaque, // void* in/out
    ServerName: [*c]u8, // BYTE*
    NameLength1: i16, // SHORT
    UserName: [*c]u8, // BYTE*
    NameLength2: i16, // SHORT
    Authentication: [*c]u8, // BYTE*
    NameLength3: i16 // SHORT
) callconv(std.os.windows.WINAPI) i16;
proc SQLConnect(
    ConnectionHandle: pointer,  # void* in/out
    ServerName: ptr uint8,  # BYTE*
    NameLength1: int16,  # SHORT
    UserName: ptr uint8,  # BYTE*
    NameLength2: int16,  # SHORT
    Authentication: ptr uint8,  # BYTE*
    NameLength3: int16  # SHORT
): int16 {.importc: "SQLConnect", stdcall, dynlib: "ODBC32.dll".}
pragma(lib, "odbc32");
extern(Windows)
short SQLConnect(
    void* ConnectionHandle,   // void* in/out
    ubyte* ServerName,   // BYTE*
    short NameLength1,   // SHORT
    ubyte* UserName,   // BYTE*
    short NameLength2,   // SHORT
    ubyte* Authentication,   // BYTE*
    short NameLength3   // SHORT
);
ccall((:SQLConnect, "ODBC32.dll"), stdcall, Int16,
      (Ptr{Cvoid}, Ptr{UInt8}, Int16, Ptr{UInt8}, Int16, Ptr{UInt8}, Int16),
      ConnectionHandle, ServerName, NameLength1, UserName, NameLength2, Authentication, NameLength3)
# ConnectionHandle : void* in/out -> Ptr{Cvoid}
# ServerName : BYTE* -> Ptr{UInt8}
# NameLength1 : SHORT -> Int16
# UserName : BYTE* -> Ptr{UInt8}
# NameLength2 : SHORT -> Int16
# Authentication : BYTE* -> Ptr{UInt8}
# NameLength3 : SHORT -> Int16
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int16_t SQLConnect(
    void* ConnectionHandle,
    uint8_t* ServerName,
    int16_t NameLength1,
    uint8_t* UserName,
    int16_t NameLength2,
    uint8_t* Authentication,
    int16_t NameLength3);
]]
local odbc32 = ffi.load("odbc32")
-- odbc32.SQLConnect(ConnectionHandle, ServerName, NameLength1, UserName, NameLength2, Authentication, NameLength3)
-- ConnectionHandle : void* in/out
-- ServerName : BYTE*
-- NameLength1 : SHORT
-- UserName : BYTE*
-- NameLength2 : SHORT
-- Authentication : BYTE*
-- NameLength3 : SHORT
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('ODBC32.dll');
const SQLConnect = lib.func('__stdcall', 'SQLConnect', 'int16_t', ['void *', 'uint8_t *', 'int16_t', 'uint8_t *', 'int16_t', 'uint8_t *', 'int16_t']);
// SQLConnect(ConnectionHandle, ServerName, NameLength1, UserName, NameLength2, Authentication, NameLength3)
// ConnectionHandle : void* in/out -> 'void *'
// ServerName : BYTE* -> 'uint8_t *'
// NameLength1 : SHORT -> 'int16_t'
// UserName : BYTE* -> 'uint8_t *'
// NameLength2 : SHORT -> 'int16_t'
// Authentication : BYTE* -> 'uint8_t *'
// NameLength3 : SHORT -> 'int16_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("ODBC32.dll", {
  SQLConnect: { parameters: ["pointer", "pointer", "i16", "pointer", "i16", "pointer", "i16"], result: "i16" },
});
// lib.symbols.SQLConnect(ConnectionHandle, ServerName, NameLength1, UserName, NameLength2, Authentication, NameLength3)
// ConnectionHandle : void* in/out -> "pointer"
// ServerName : BYTE* -> "pointer"
// NameLength1 : SHORT -> "i16"
// UserName : BYTE* -> "pointer"
// NameLength2 : SHORT -> "i16"
// Authentication : BYTE* -> "pointer"
// NameLength3 : SHORT -> "i16"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int16_t SQLConnect(
    void* ConnectionHandle,
    uint8_t* ServerName,
    int16_t NameLength1,
    uint8_t* UserName,
    int16_t NameLength2,
    uint8_t* Authentication,
    int16_t NameLength3);
C, "ODBC32.dll");
// $ffi->SQLConnect(ConnectionHandle, ServerName, NameLength1, UserName, NameLength2, Authentication, NameLength3);
// ConnectionHandle : void* in/out
// ServerName : BYTE*
// NameLength1 : SHORT
// UserName : BYTE*
// NameLength2 : SHORT
// Authentication : BYTE*
// NameLength3 : SHORT
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Odbc32 extends StdCallLibrary {
    Odbc32 INSTANCE = Native.load("odbc32", Odbc32.class, W32APIOptions.ASCII_OPTIONS);
    short SQLConnect(
        Pointer ConnectionHandle,   // void* in/out
        byte[] ServerName,   // BYTE*
        short NameLength1,   // SHORT
        byte[] UserName,   // BYTE*
        short NameLength2,   // SHORT
        byte[] Authentication,   // BYTE*
        short NameLength3   // SHORT
    );
}
@[Link("odbc32")]
lib LibODBC32
  fun SQLConnect = SQLConnect(
    ConnectionHandle : Void*,   # void* in/out
    ServerName : UInt8*,   # BYTE*
    NameLength1 : Int16,   # SHORT
    UserName : UInt8*,   # BYTE*
    NameLength2 : Int16,   # SHORT
    Authentication : UInt8*,   # BYTE*
    NameLength3 : Int16   # SHORT
  ) : Int16
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef SQLConnectNative = Int16 Function(Pointer<Void>, Pointer<Uint8>, Int16, Pointer<Uint8>, Int16, Pointer<Uint8>, Int16);
typedef SQLConnectDart = int Function(Pointer<Void>, Pointer<Uint8>, int, Pointer<Uint8>, int, Pointer<Uint8>, int);
final SQLConnect = DynamicLibrary.open('ODBC32.dll')
    .lookupFunction<SQLConnectNative, SQLConnectDart>('SQLConnect');
// ConnectionHandle : void* in/out -> Pointer<Void>
// ServerName : BYTE* -> Pointer<Uint8>
// NameLength1 : SHORT -> Int16
// UserName : BYTE* -> Pointer<Uint8>
// NameLength2 : SHORT -> Int16
// Authentication : BYTE* -> Pointer<Uint8>
// NameLength3 : SHORT -> Int16
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SQLConnect(
  ConnectionHandle: Pointer;   // void* in/out
  ServerName: Pointer;   // BYTE*
  NameLength1: Smallint;   // SHORT
  UserName: Pointer;   // BYTE*
  NameLength2: Smallint;   // SHORT
  Authentication: Pointer;   // BYTE*
  NameLength3: Smallint   // SHORT
): Smallint; stdcall;
  external 'ODBC32.dll' name 'SQLConnect';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SQLConnect"
  c_SQLConnect :: Ptr () -> Ptr Word8 -> Int16 -> Ptr Word8 -> Int16 -> Ptr Word8 -> Int16 -> IO Int16
-- ConnectionHandle : void* in/out -> Ptr ()
-- ServerName : BYTE* -> Ptr Word8
-- NameLength1 : SHORT -> Int16
-- UserName : BYTE* -> Ptr Word8
-- NameLength2 : SHORT -> Int16
-- Authentication : BYTE* -> Ptr Word8
-- NameLength3 : SHORT -> Int16
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let sqlconnect =
  foreign "SQLConnect"
    ((ptr void) @-> (ptr uint8_t) @-> int16_t @-> (ptr uint8_t) @-> int16_t @-> (ptr uint8_t) @-> int16_t @-> returning int16_t)
(* ConnectionHandle : void* in/out -> (ptr void) *)
(* ServerName : BYTE* -> (ptr uint8_t) *)
(* NameLength1 : SHORT -> int16_t *)
(* UserName : BYTE* -> (ptr uint8_t) *)
(* NameLength2 : SHORT -> int16_t *)
(* Authentication : BYTE* -> (ptr uint8_t) *)
(* NameLength3 : SHORT -> int16_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library odbc32 (t "ODBC32.dll"))
(cffi:use-foreign-library odbc32)

(cffi:defcfun ("SQLConnect" sqlconnect :convention :stdcall) :int16
  (connection-handle :pointer)   ; void* in/out
  (server-name :pointer)   ; BYTE*
  (name-length1 :int16)   ; SHORT
  (user-name :pointer)   ; BYTE*
  (name-length2 :int16)   ; SHORT
  (authentication :pointer)   ; BYTE*
  (name-length3 :int16))   ; SHORT
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SQLConnect = Win32::API::More->new('ODBC32',
    'short SQLConnect(LPVOID ConnectionHandle, LPVOID ServerName, short NameLength1, LPVOID UserName, short NameLength2, LPVOID Authentication, short NameLength3)');
# my $ret = $SQLConnect->Call($ConnectionHandle, $ServerName, $NameLength1, $UserName, $NameLength2, $Authentication, $NameLength3);
# ConnectionHandle : void* in/out -> LPVOID
# ServerName : BYTE* -> LPVOID
# NameLength1 : SHORT -> short
# UserName : BYTE* -> LPVOID
# NameLength2 : SHORT -> short
# Authentication : BYTE* -> LPVOID
# NameLength3 : SHORT -> short
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い