ホーム › System.Search › SQLConnectW
SQLConnectW
関数DSN名とユーザー認証でデータソースに接続する(Unicode)。
シグネチャ
// ODBC32.dll (Unicode / -W)
#include <windows.h>
SHORT SQLConnectW(
void* hdbc,
WORD* szDSN,
SHORT cchDSN,
WORD* szUID,
SHORT cchUID,
WORD* szAuthStr,
SHORT cchAuthStr
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hdbc | void* | inout | 接続を確立する接続ハンドル。 |
| szDSN | WORD* | in | データソース名(DSN)。Unicode文字列。 |
| cchDSN | SHORT | in | szDSNの文字数。SQL_NTSでNUL終端を示す。 |
| szUID | WORD* | in | ユーザーID(認証名)。Unicode文字列。NULL可。 |
| cchUID | SHORT | in | szUIDの文字数。SQL_NTSでNUL終端を示す。 |
| szAuthStr | WORD* | in | 認証文字列(パスワード)。Unicode文字列。NULL可。 |
| cchAuthStr | SHORT | in | szAuthStrの文字数。SQL_NTSでNUL終端を示す。 |
戻り値の型: SHORT
各言語での呼び出し定義
// ODBC32.dll (Unicode / -W)
#include <windows.h>
SHORT SQLConnectW(
void* hdbc,
WORD* szDSN,
SHORT cchDSN,
WORD* szUID,
SHORT cchUID,
WORD* szAuthStr,
SHORT cchAuthStr
);[DllImport("ODBC32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern short SQLConnectW(
IntPtr hdbc, // void* in/out
ref ushort szDSN, // WORD*
short cchDSN, // SHORT
ref ushort szUID, // WORD*
short cchUID, // SHORT
ref ushort szAuthStr, // WORD*
short cchAuthStr // SHORT
);<DllImport("ODBC32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function SQLConnectW(
hdbc As IntPtr, ' void* in/out
ByRef szDSN As UShort, ' WORD*
cchDSN As Short, ' SHORT
ByRef szUID As UShort, ' WORD*
cchUID As Short, ' SHORT
ByRef szAuthStr As UShort, ' WORD*
cchAuthStr As Short ' SHORT
) As Short
End Function' hdbc : void* in/out
' szDSN : WORD*
' cchDSN : SHORT
' szUID : WORD*
' cchUID : SHORT
' szAuthStr : WORD*
' cchAuthStr : SHORT
Declare PtrSafe Function SQLConnectW Lib "odbc32" ( _
ByVal hdbc As LongPtr, _
ByRef szDSN As Integer, _
ByVal cchDSN As Integer, _
ByRef szUID As Integer, _
ByVal cchUID As Integer, _
ByRef szAuthStr As Integer, _
ByVal cchAuthStr As Integer) As Integer
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
SQLConnectW = ctypes.windll.odbc32.SQLConnectW
SQLConnectW.restype = ctypes.c_short
SQLConnectW.argtypes = [
ctypes.POINTER(None), # hdbc : void* in/out
ctypes.POINTER(ctypes.c_ushort), # szDSN : WORD*
ctypes.c_short, # cchDSN : SHORT
ctypes.POINTER(ctypes.c_ushort), # szUID : WORD*
ctypes.c_short, # cchUID : SHORT
ctypes.POINTER(ctypes.c_ushort), # szAuthStr : WORD*
ctypes.c_short, # cchAuthStr : SHORT
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('ODBC32.dll')
SQLConnectW = Fiddle::Function.new(
lib['SQLConnectW'],
[
Fiddle::TYPE_VOIDP, # hdbc : void* in/out
Fiddle::TYPE_VOIDP, # szDSN : WORD*
Fiddle::TYPE_SHORT, # cchDSN : SHORT
Fiddle::TYPE_VOIDP, # szUID : WORD*
Fiddle::TYPE_SHORT, # cchUID : SHORT
Fiddle::TYPE_VOIDP, # szAuthStr : WORD*
Fiddle::TYPE_SHORT, # cchAuthStr : SHORT
],
Fiddle::TYPE_SHORT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "odbc32")]
extern "system" {
fn SQLConnectW(
hdbc: *mut (), // void* in/out
szDSN: *mut u16, // WORD*
cchDSN: i16, // SHORT
szUID: *mut u16, // WORD*
cchUID: i16, // SHORT
szAuthStr: *mut u16, // WORD*
cchAuthStr: i16 // SHORT
) -> i16;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("ODBC32.dll", CharSet = CharSet.Unicode)]
public static extern short SQLConnectW(IntPtr hdbc, ref ushort szDSN, short cchDSN, ref ushort szUID, short cchUID, ref ushort szAuthStr, short cchAuthStr);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ODBC32_SQLConnectW' -Namespace Win32 -PassThru
# $api::SQLConnectW(hdbc, szDSN, cchDSN, szUID, cchUID, szAuthStr, cchAuthStr)#uselib "ODBC32.dll"
#func global SQLConnectW "SQLConnectW" wptr, wptr, wptr, wptr, wptr, wptr, wptr
; SQLConnectW hdbc, varptr(szDSN), cchDSN, varptr(szUID), cchUID, varptr(szAuthStr), cchAuthStr ; 戻り値は stat
; hdbc : void* in/out -> "wptr"
; szDSN : WORD* -> "wptr"
; cchDSN : SHORT -> "wptr"
; szUID : WORD* -> "wptr"
; cchUID : SHORT -> "wptr"
; szAuthStr : WORD* -> "wptr"
; cchAuthStr : SHORT -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "ODBC32.dll" #cfunc global SQLConnectW "SQLConnectW" sptr, var, int, var, int, var, int ; res = SQLConnectW(hdbc, szDSN, cchDSN, szUID, cchUID, szAuthStr, cchAuthStr) ; hdbc : void* in/out -> "sptr" ; szDSN : WORD* -> "var" ; cchDSN : SHORT -> "int" ; szUID : WORD* -> "var" ; cchUID : SHORT -> "int" ; szAuthStr : WORD* -> "var" ; cchAuthStr : SHORT -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "ODBC32.dll" #cfunc global SQLConnectW "SQLConnectW" sptr, sptr, int, sptr, int, sptr, int ; res = SQLConnectW(hdbc, varptr(szDSN), cchDSN, varptr(szUID), cchUID, varptr(szAuthStr), cchAuthStr) ; hdbc : void* in/out -> "sptr" ; szDSN : WORD* -> "sptr" ; cchDSN : SHORT -> "int" ; szUID : WORD* -> "sptr" ; cchUID : SHORT -> "int" ; szAuthStr : WORD* -> "sptr" ; cchAuthStr : SHORT -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; SHORT SQLConnectW(void* hdbc, WORD* szDSN, SHORT cchDSN, WORD* szUID, SHORT cchUID, WORD* szAuthStr, SHORT cchAuthStr) #uselib "ODBC32.dll" #cfunc global SQLConnectW "SQLConnectW" intptr, var, int, var, int, var, int ; res = SQLConnectW(hdbc, szDSN, cchDSN, szUID, cchUID, szAuthStr, cchAuthStr) ; hdbc : void* in/out -> "intptr" ; szDSN : WORD* -> "var" ; cchDSN : SHORT -> "int" ; szUID : WORD* -> "var" ; cchUID : SHORT -> "int" ; szAuthStr : WORD* -> "var" ; cchAuthStr : SHORT -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; SHORT SQLConnectW(void* hdbc, WORD* szDSN, SHORT cchDSN, WORD* szUID, SHORT cchUID, WORD* szAuthStr, SHORT cchAuthStr) #uselib "ODBC32.dll" #cfunc global SQLConnectW "SQLConnectW" intptr, intptr, int, intptr, int, intptr, int ; res = SQLConnectW(hdbc, varptr(szDSN), cchDSN, varptr(szUID), cchUID, varptr(szAuthStr), cchAuthStr) ; hdbc : void* in/out -> "intptr" ; szDSN : WORD* -> "intptr" ; cchDSN : SHORT -> "int" ; szUID : WORD* -> "intptr" ; cchUID : SHORT -> "int" ; szAuthStr : WORD* -> "intptr" ; cchAuthStr : SHORT -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
odbc32 = windows.NewLazySystemDLL("ODBC32.dll")
procSQLConnectW = odbc32.NewProc("SQLConnectW")
)
// hdbc (void* in/out), szDSN (WORD*), cchDSN (SHORT), szUID (WORD*), cchUID (SHORT), szAuthStr (WORD*), cchAuthStr (SHORT)
r1, _, err := procSQLConnectW.Call(
uintptr(hdbc),
uintptr(szDSN),
uintptr(cchDSN),
uintptr(szUID),
uintptr(cchUID),
uintptr(szAuthStr),
uintptr(cchAuthStr),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // SHORTfunction SQLConnectW(
hdbc: Pointer; // void* in/out
szDSN: Pointer; // WORD*
cchDSN: Smallint; // SHORT
szUID: Pointer; // WORD*
cchUID: Smallint; // SHORT
szAuthStr: Pointer; // WORD*
cchAuthStr: Smallint // SHORT
): Smallint; stdcall;
external 'ODBC32.dll' name 'SQLConnectW';result := DllCall("ODBC32\SQLConnectW"
, "Ptr", hdbc ; void* in/out
, "Ptr", szDSN ; WORD*
, "Short", cchDSN ; SHORT
, "Ptr", szUID ; WORD*
, "Short", cchUID ; SHORT
, "Ptr", szAuthStr ; WORD*
, "Short", cchAuthStr ; SHORT
, "Short") ; return: SHORT●SQLConnectW(hdbc, szDSN, cchDSN, szUID, cchUID, szAuthStr, cchAuthStr) = DLL("ODBC32.dll", "int SQLConnectW(void*, void*, int, void*, int, void*, int)")
# 呼び出し: SQLConnectW(hdbc, szDSN, cchDSN, szUID, cchUID, szAuthStr, cchAuthStr)
# hdbc : void* in/out -> "void*"
# szDSN : WORD* -> "void*"
# cchDSN : SHORT -> "int"
# szUID : WORD* -> "void*"
# cchUID : SHORT -> "int"
# szAuthStr : WORD* -> "void*"
# cchAuthStr : SHORT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "odbc32" fn SQLConnectW(
hdbc: ?*anyopaque, // void* in/out
szDSN: [*c]u16, // WORD*
cchDSN: i16, // SHORT
szUID: [*c]u16, // WORD*
cchUID: i16, // SHORT
szAuthStr: [*c]u16, // WORD*
cchAuthStr: i16 // SHORT
) callconv(std.os.windows.WINAPI) i16;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc SQLConnectW(
hdbc: pointer, # void* in/out
szDSN: ptr uint16, # WORD*
cchDSN: int16, # SHORT
szUID: ptr uint16, # WORD*
cchUID: int16, # SHORT
szAuthStr: ptr uint16, # WORD*
cchAuthStr: int16 # SHORT
): int16 {.importc: "SQLConnectW", stdcall, dynlib: "ODBC32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "odbc32");
extern(Windows)
short SQLConnectW(
void* hdbc, // void* in/out
ushort* szDSN, // WORD*
short cchDSN, // SHORT
ushort* szUID, // WORD*
short cchUID, // SHORT
ushort* szAuthStr, // WORD*
short cchAuthStr // SHORT
);ccall((:SQLConnectW, "ODBC32.dll"), stdcall, Int16,
(Ptr{Cvoid}, Ptr{UInt16}, Int16, Ptr{UInt16}, Int16, Ptr{UInt16}, Int16),
hdbc, szDSN, cchDSN, szUID, cchUID, szAuthStr, cchAuthStr)
# hdbc : void* in/out -> Ptr{Cvoid}
# szDSN : WORD* -> Ptr{UInt16}
# cchDSN : SHORT -> Int16
# szUID : WORD* -> Ptr{UInt16}
# cchUID : SHORT -> Int16
# szAuthStr : WORD* -> Ptr{UInt16}
# cchAuthStr : SHORT -> Int16
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
int16_t SQLConnectW(
void* hdbc,
uint16_t* szDSN,
int16_t cchDSN,
uint16_t* szUID,
int16_t cchUID,
uint16_t* szAuthStr,
int16_t cchAuthStr);
]]
local odbc32 = ffi.load("odbc32")
-- odbc32.SQLConnectW(hdbc, szDSN, cchDSN, szUID, cchUID, szAuthStr, cchAuthStr)
-- hdbc : void* in/out
-- szDSN : WORD*
-- cchDSN : SHORT
-- szUID : WORD*
-- cchUID : SHORT
-- szAuthStr : WORD*
-- cchAuthStr : SHORT
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('ODBC32.dll');
const SQLConnectW = lib.func('__stdcall', 'SQLConnectW', 'int16_t', ['void *', 'uint16_t *', 'int16_t', 'uint16_t *', 'int16_t', 'uint16_t *', 'int16_t']);
// SQLConnectW(hdbc, szDSN, cchDSN, szUID, cchUID, szAuthStr, cchAuthStr)
// hdbc : void* in/out -> 'void *'
// szDSN : WORD* -> 'uint16_t *'
// cchDSN : SHORT -> 'int16_t'
// szUID : WORD* -> 'uint16_t *'
// cchUID : SHORT -> 'int16_t'
// szAuthStr : WORD* -> 'uint16_t *'
// cchAuthStr : SHORT -> 'int16_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("ODBC32.dll", {
SQLConnectW: { parameters: ["pointer", "pointer", "i16", "pointer", "i16", "pointer", "i16"], result: "i16" },
});
// lib.symbols.SQLConnectW(hdbc, szDSN, cchDSN, szUID, cchUID, szAuthStr, cchAuthStr)
// hdbc : void* in/out -> "pointer"
// szDSN : WORD* -> "pointer"
// cchDSN : SHORT -> "i16"
// szUID : WORD* -> "pointer"
// cchUID : SHORT -> "i16"
// szAuthStr : WORD* -> "pointer"
// cchAuthStr : SHORT -> "i16"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int16_t SQLConnectW(
void* hdbc,
uint16_t* szDSN,
int16_t cchDSN,
uint16_t* szUID,
int16_t cchUID,
uint16_t* szAuthStr,
int16_t cchAuthStr);
C, "ODBC32.dll");
// $ffi->SQLConnectW(hdbc, szDSN, cchDSN, szUID, cchUID, szAuthStr, cchAuthStr);
// hdbc : void* in/out
// szDSN : WORD*
// cchDSN : SHORT
// szUID : WORD*
// cchUID : SHORT
// szAuthStr : WORD*
// cchAuthStr : 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.UNICODE_OPTIONS);
short SQLConnectW(
Pointer hdbc, // void* in/out
ShortByReference szDSN, // WORD*
short cchDSN, // SHORT
ShortByReference szUID, // WORD*
short cchUID, // SHORT
ShortByReference szAuthStr, // WORD*
short cchAuthStr // SHORT
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("odbc32")]
lib LibODBC32
fun SQLConnectW = SQLConnectW(
hdbc : Void*, # void* in/out
szDSN : UInt16*, # WORD*
cchDSN : Int16, # SHORT
szUID : UInt16*, # WORD*
cchUID : Int16, # SHORT
szAuthStr : UInt16*, # WORD*
cchAuthStr : 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 SQLConnectWNative = Int16 Function(Pointer<Void>, Pointer<Uint16>, Int16, Pointer<Uint16>, Int16, Pointer<Uint16>, Int16);
typedef SQLConnectWDart = int Function(Pointer<Void>, Pointer<Uint16>, int, Pointer<Uint16>, int, Pointer<Uint16>, int);
final SQLConnectW = DynamicLibrary.open('ODBC32.dll')
.lookupFunction<SQLConnectWNative, SQLConnectWDart>('SQLConnectW');
// hdbc : void* in/out -> Pointer<Void>
// szDSN : WORD* -> Pointer<Uint16>
// cchDSN : SHORT -> Int16
// szUID : WORD* -> Pointer<Uint16>
// cchUID : SHORT -> Int16
// szAuthStr : WORD* -> Pointer<Uint16>
// cchAuthStr : SHORT -> Int16
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function SQLConnectW(
hdbc: Pointer; // void* in/out
szDSN: Pointer; // WORD*
cchDSN: Smallint; // SHORT
szUID: Pointer; // WORD*
cchUID: Smallint; // SHORT
szAuthStr: Pointer; // WORD*
cchAuthStr: Smallint // SHORT
): Smallint; stdcall;
external 'ODBC32.dll' name 'SQLConnectW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "SQLConnectW"
c_SQLConnectW :: Ptr () -> Ptr Word16 -> Int16 -> Ptr Word16 -> Int16 -> Ptr Word16 -> Int16 -> IO Int16
-- hdbc : void* in/out -> Ptr ()
-- szDSN : WORD* -> Ptr Word16
-- cchDSN : SHORT -> Int16
-- szUID : WORD* -> Ptr Word16
-- cchUID : SHORT -> Int16
-- szAuthStr : WORD* -> Ptr Word16
-- cchAuthStr : SHORT -> Int16
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let sqlconnectw =
foreign "SQLConnectW"
((ptr void) @-> (ptr uint16_t) @-> int16_t @-> (ptr uint16_t) @-> int16_t @-> (ptr uint16_t) @-> int16_t @-> returning int16_t)
(* hdbc : void* in/out -> (ptr void) *)
(* szDSN : WORD* -> (ptr uint16_t) *)
(* cchDSN : SHORT -> int16_t *)
(* szUID : WORD* -> (ptr uint16_t) *)
(* cchUID : SHORT -> int16_t *)
(* szAuthStr : WORD* -> (ptr uint16_t) *)
(* cchAuthStr : 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 ("SQLConnectW" sqlconnect-w :convention :stdcall) :int16
(hdbc :pointer) ; void* in/out
(sz-dsn :pointer) ; WORD*
(cch-dsn :int16) ; SHORT
(sz-uid :pointer) ; WORD*
(cch-uid :int16) ; SHORT
(sz-auth-str :pointer) ; WORD*
(cch-auth-str :int16)) ; SHORT
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $SQLConnectW = Win32::API::More->new('ODBC32',
'short SQLConnectW(LPVOID hdbc, LPVOID szDSN, short cchDSN, LPVOID szUID, short cchUID, LPVOID szAuthStr, short cchAuthStr)');
# my $ret = $SQLConnectW->Call($hdbc, $szDSN, $cchDSN, $szUID, $cchUID, $szAuthStr, $cchAuthStr);
# hdbc : void* in/out -> LPVOID
# szDSN : WORD* -> LPVOID
# cchDSN : SHORT -> short
# szUID : WORD* -> LPVOID
# cchUID : SHORT -> short
# szAuthStr : WORD* -> LPVOID
# cchAuthStr : SHORT -> short
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
文字セット違い
- f SQLConnectA (ANSI版) — DSN名とユーザー認証でデータソースに接続する(ANSI)。