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

SQLGetDiagRecW

関数
診断レコードのSQLState・エラー情報を取得する(Unicode)。
DLLODBC32.dll文字セットUnicode (-W)呼出規約winapi

シグネチャ

// ODBC32.dll  (Unicode / -W)
#include <windows.h>

SHORT SQLGetDiagRecW(
    SHORT fHandleType,
    void* handle,
    SHORT iRecord,
    WORD* szSqlState,   // optional
    INT* pfNativeError,
    WORD* szErrorMsg,   // optional
    SHORT cchErrorMsgMax,
    SHORT* pcchErrorMsg
);

パラメーター

名前方向説明
fHandleTypeSHORTin診断情報を取得するハンドルの種別。SQL_HANDLE_*を指定する。
handlevoid*inout診断情報の対象となるハンドル。
iRecordSHORTin取得する診断レコード番号。1始まりで指定する。
szSqlStateWORD*outoptional5文字のSQLSTATEコードを受け取るバッファ。Unicode文字列。
pfNativeErrorINT*inoutデータソース固有のネイティブエラーコードを受け取るポインタ。
szErrorMsgWORD*outoptionalエラーメッセージを受け取るバッファ。Unicode文字列。
cchErrorMsgMaxSHORTinszErrorMsgバッファの文字数長。
pcchErrorMsgSHORT*inoutメッセージの実際の文字数を受け取るポインタ。NULL可。

戻り値の型: SHORT

各言語での呼び出し定義

// ODBC32.dll  (Unicode / -W)
#include <windows.h>

SHORT SQLGetDiagRecW(
    SHORT fHandleType,
    void* handle,
    SHORT iRecord,
    WORD* szSqlState,   // optional
    INT* pfNativeError,
    WORD* szErrorMsg,   // optional
    SHORT cchErrorMsgMax,
    SHORT* pcchErrorMsg
);
[DllImport("ODBC32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern short SQLGetDiagRecW(
    short fHandleType,   // SHORT
    IntPtr handle,   // void* in/out
    short iRecord,   // SHORT
    IntPtr szSqlState,   // WORD* optional, out
    ref int pfNativeError,   // INT* in/out
    IntPtr szErrorMsg,   // WORD* optional, out
    short cchErrorMsgMax,   // SHORT
    ref short pcchErrorMsg   // SHORT* in/out
);
<DllImport("ODBC32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function SQLGetDiagRecW(
    fHandleType As Short,   ' SHORT
    handle As IntPtr,   ' void* in/out
    iRecord As Short,   ' SHORT
    szSqlState As IntPtr,   ' WORD* optional, out
    ByRef pfNativeError As Integer,   ' INT* in/out
    szErrorMsg As IntPtr,   ' WORD* optional, out
    cchErrorMsgMax As Short,   ' SHORT
    ByRef pcchErrorMsg As Short   ' SHORT* in/out
) As Short
End Function
' fHandleType : SHORT
' handle : void* in/out
' iRecord : SHORT
' szSqlState : WORD* optional, out
' pfNativeError : INT* in/out
' szErrorMsg : WORD* optional, out
' cchErrorMsgMax : SHORT
' pcchErrorMsg : SHORT* in/out
Declare PtrSafe Function SQLGetDiagRecW Lib "odbc32" ( _
    ByVal fHandleType As Integer, _
    ByVal handle As LongPtr, _
    ByVal iRecord As Integer, _
    ByVal szSqlState As LongPtr, _
    ByRef pfNativeError As Long, _
    ByVal szErrorMsg As LongPtr, _
    ByVal cchErrorMsgMax As Integer, _
    ByRef pcchErrorMsg 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

SQLGetDiagRecW = ctypes.windll.odbc32.SQLGetDiagRecW
SQLGetDiagRecW.restype = ctypes.c_short
SQLGetDiagRecW.argtypes = [
    ctypes.c_short,  # fHandleType : SHORT
    ctypes.POINTER(None),  # handle : void* in/out
    ctypes.c_short,  # iRecord : SHORT
    ctypes.POINTER(ctypes.c_ushort),  # szSqlState : WORD* optional, out
    ctypes.POINTER(ctypes.c_int),  # pfNativeError : INT* in/out
    ctypes.POINTER(ctypes.c_ushort),  # szErrorMsg : WORD* optional, out
    ctypes.c_short,  # cchErrorMsgMax : SHORT
    ctypes.POINTER(ctypes.c_short),  # pcchErrorMsg : SHORT* in/out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ODBC32.dll')
SQLGetDiagRecW = Fiddle::Function.new(
  lib['SQLGetDiagRecW'],
  [
    Fiddle::TYPE_SHORT,  # fHandleType : SHORT
    Fiddle::TYPE_VOIDP,  # handle : void* in/out
    Fiddle::TYPE_SHORT,  # iRecord : SHORT
    Fiddle::TYPE_VOIDP,  # szSqlState : WORD* optional, out
    Fiddle::TYPE_VOIDP,  # pfNativeError : INT* in/out
    Fiddle::TYPE_VOIDP,  # szErrorMsg : WORD* optional, out
    Fiddle::TYPE_SHORT,  # cchErrorMsgMax : SHORT
    Fiddle::TYPE_VOIDP,  # pcchErrorMsg : SHORT* in/out
  ],
  Fiddle::TYPE_SHORT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "odbc32")]
extern "system" {
    fn SQLGetDiagRecW(
        fHandleType: i16,  // SHORT
        handle: *mut (),  // void* in/out
        iRecord: i16,  // SHORT
        szSqlState: *mut u16,  // WORD* optional, out
        pfNativeError: *mut i32,  // INT* in/out
        szErrorMsg: *mut u16,  // WORD* optional, out
        cchErrorMsgMax: i16,  // SHORT
        pcchErrorMsg: *mut i16  // SHORT* in/out
    ) -> i16;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("ODBC32.dll", CharSet = CharSet.Unicode)]
public static extern short SQLGetDiagRecW(short fHandleType, IntPtr handle, short iRecord, IntPtr szSqlState, ref int pfNativeError, IntPtr szErrorMsg, short cchErrorMsgMax, ref short pcchErrorMsg);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ODBC32_SQLGetDiagRecW' -Namespace Win32 -PassThru
# $api::SQLGetDiagRecW(fHandleType, handle, iRecord, szSqlState, pfNativeError, szErrorMsg, cchErrorMsgMax, pcchErrorMsg)
#uselib "ODBC32.dll"
#func global SQLGetDiagRecW "SQLGetDiagRecW" wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr
; SQLGetDiagRecW fHandleType, handle, iRecord, varptr(szSqlState), varptr(pfNativeError), varptr(szErrorMsg), cchErrorMsgMax, varptr(pcchErrorMsg)   ; 戻り値は stat
; fHandleType : SHORT -> "wptr"
; handle : void* in/out -> "wptr"
; iRecord : SHORT -> "wptr"
; szSqlState : WORD* optional, out -> "wptr"
; pfNativeError : INT* in/out -> "wptr"
; szErrorMsg : WORD* optional, out -> "wptr"
; cchErrorMsgMax : SHORT -> "wptr"
; pcchErrorMsg : SHORT* in/out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ODBC32.dll"
#cfunc global SQLGetDiagRecW "SQLGetDiagRecW" int, sptr, int, var, var, var, int, var
; res = SQLGetDiagRecW(fHandleType, handle, iRecord, szSqlState, pfNativeError, szErrorMsg, cchErrorMsgMax, pcchErrorMsg)
; fHandleType : SHORT -> "int"
; handle : void* in/out -> "sptr"
; iRecord : SHORT -> "int"
; szSqlState : WORD* optional, out -> "var"
; pfNativeError : INT* in/out -> "var"
; szErrorMsg : WORD* optional, out -> "var"
; cchErrorMsgMax : SHORT -> "int"
; pcchErrorMsg : SHORT* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; SHORT SQLGetDiagRecW(SHORT fHandleType, void* handle, SHORT iRecord, WORD* szSqlState, INT* pfNativeError, WORD* szErrorMsg, SHORT cchErrorMsgMax, SHORT* pcchErrorMsg)
#uselib "ODBC32.dll"
#cfunc global SQLGetDiagRecW "SQLGetDiagRecW" int, intptr, int, var, var, var, int, var
; res = SQLGetDiagRecW(fHandleType, handle, iRecord, szSqlState, pfNativeError, szErrorMsg, cchErrorMsgMax, pcchErrorMsg)
; fHandleType : SHORT -> "int"
; handle : void* in/out -> "intptr"
; iRecord : SHORT -> "int"
; szSqlState : WORD* optional, out -> "var"
; pfNativeError : INT* in/out -> "var"
; szErrorMsg : WORD* optional, out -> "var"
; cchErrorMsgMax : SHORT -> "int"
; pcchErrorMsg : SHORT* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	odbc32 = windows.NewLazySystemDLL("ODBC32.dll")
	procSQLGetDiagRecW = odbc32.NewProc("SQLGetDiagRecW")
)

// fHandleType (SHORT), handle (void* in/out), iRecord (SHORT), szSqlState (WORD* optional, out), pfNativeError (INT* in/out), szErrorMsg (WORD* optional, out), cchErrorMsgMax (SHORT), pcchErrorMsg (SHORT* in/out)
r1, _, err := procSQLGetDiagRecW.Call(
	uintptr(fHandleType),
	uintptr(handle),
	uintptr(iRecord),
	uintptr(szSqlState),
	uintptr(pfNativeError),
	uintptr(szErrorMsg),
	uintptr(cchErrorMsgMax),
	uintptr(pcchErrorMsg),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // SHORT
function SQLGetDiagRecW(
  fHandleType: Smallint;   // SHORT
  handle: Pointer;   // void* in/out
  iRecord: Smallint;   // SHORT
  szSqlState: Pointer;   // WORD* optional, out
  pfNativeError: Pointer;   // INT* in/out
  szErrorMsg: Pointer;   // WORD* optional, out
  cchErrorMsgMax: Smallint;   // SHORT
  pcchErrorMsg: Pointer   // SHORT* in/out
): Smallint; stdcall;
  external 'ODBC32.dll' name 'SQLGetDiagRecW';
result := DllCall("ODBC32\SQLGetDiagRecW"
    , "Short", fHandleType   ; SHORT
    , "Ptr", handle   ; void* in/out
    , "Short", iRecord   ; SHORT
    , "Ptr", szSqlState   ; WORD* optional, out
    , "Ptr", pfNativeError   ; INT* in/out
    , "Ptr", szErrorMsg   ; WORD* optional, out
    , "Short", cchErrorMsgMax   ; SHORT
    , "Ptr", pcchErrorMsg   ; SHORT* in/out
    , "Short")   ; return: SHORT
●SQLGetDiagRecW(fHandleType, handle, iRecord, szSqlState, pfNativeError, szErrorMsg, cchErrorMsgMax, pcchErrorMsg) = DLL("ODBC32.dll", "int SQLGetDiagRecW(int, void*, int, void*, void*, void*, int, void*)")
# 呼び出し: SQLGetDiagRecW(fHandleType, handle, iRecord, szSqlState, pfNativeError, szErrorMsg, cchErrorMsgMax, pcchErrorMsg)
# fHandleType : SHORT -> "int"
# handle : void* in/out -> "void*"
# iRecord : SHORT -> "int"
# szSqlState : WORD* optional, out -> "void*"
# pfNativeError : INT* in/out -> "void*"
# szErrorMsg : WORD* optional, out -> "void*"
# cchErrorMsgMax : SHORT -> "int"
# pcchErrorMsg : SHORT* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "odbc32" fn SQLGetDiagRecW(
    fHandleType: i16, // SHORT
    handle: ?*anyopaque, // void* in/out
    iRecord: i16, // SHORT
    szSqlState: [*c]u16, // WORD* optional, out
    pfNativeError: [*c]i32, // INT* in/out
    szErrorMsg: [*c]u16, // WORD* optional, out
    cchErrorMsgMax: i16, // SHORT
    pcchErrorMsg: [*c]i16 // SHORT* in/out
) callconv(std.os.windows.WINAPI) i16;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc SQLGetDiagRecW(
    fHandleType: int16,  # SHORT
    handle: pointer,  # void* in/out
    iRecord: int16,  # SHORT
    szSqlState: ptr uint16,  # WORD* optional, out
    pfNativeError: ptr int32,  # INT* in/out
    szErrorMsg: ptr uint16,  # WORD* optional, out
    cchErrorMsgMax: int16,  # SHORT
    pcchErrorMsg: ptr int16  # SHORT* in/out
): int16 {.importc: "SQLGetDiagRecW", stdcall, dynlib: "ODBC32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "odbc32");
extern(Windows)
short SQLGetDiagRecW(
    short fHandleType,   // SHORT
    void* handle,   // void* in/out
    short iRecord,   // SHORT
    ushort* szSqlState,   // WORD* optional, out
    int* pfNativeError,   // INT* in/out
    ushort* szErrorMsg,   // WORD* optional, out
    short cchErrorMsgMax,   // SHORT
    short* pcchErrorMsg   // SHORT* in/out
);
ccall((:SQLGetDiagRecW, "ODBC32.dll"), stdcall, Int16,
      (Int16, Ptr{Cvoid}, Int16, Ptr{UInt16}, Ptr{Int32}, Ptr{UInt16}, Int16, Ptr{Int16}),
      fHandleType, handle, iRecord, szSqlState, pfNativeError, szErrorMsg, cchErrorMsgMax, pcchErrorMsg)
# fHandleType : SHORT -> Int16
# handle : void* in/out -> Ptr{Cvoid}
# iRecord : SHORT -> Int16
# szSqlState : WORD* optional, out -> Ptr{UInt16}
# pfNativeError : INT* in/out -> Ptr{Int32}
# szErrorMsg : WORD* optional, out -> Ptr{UInt16}
# cchErrorMsgMax : SHORT -> Int16
# pcchErrorMsg : SHORT* in/out -> Ptr{Int16}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
int16_t SQLGetDiagRecW(
    int16_t fHandleType,
    void* handle,
    int16_t iRecord,
    uint16_t* szSqlState,
    int32_t* pfNativeError,
    uint16_t* szErrorMsg,
    int16_t cchErrorMsgMax,
    int16_t* pcchErrorMsg);
]]
local odbc32 = ffi.load("odbc32")
-- odbc32.SQLGetDiagRecW(fHandleType, handle, iRecord, szSqlState, pfNativeError, szErrorMsg, cchErrorMsgMax, pcchErrorMsg)
-- fHandleType : SHORT
-- handle : void* in/out
-- iRecord : SHORT
-- szSqlState : WORD* optional, out
-- pfNativeError : INT* in/out
-- szErrorMsg : WORD* optional, out
-- cchErrorMsgMax : SHORT
-- pcchErrorMsg : SHORT* in/out
-- 構造体/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 SQLGetDiagRecW = lib.func('__stdcall', 'SQLGetDiagRecW', 'int16_t', ['int16_t', 'void *', 'int16_t', 'uint16_t *', 'int32_t *', 'uint16_t *', 'int16_t', 'int16_t *']);
// SQLGetDiagRecW(fHandleType, handle, iRecord, szSqlState, pfNativeError, szErrorMsg, cchErrorMsgMax, pcchErrorMsg)
// fHandleType : SHORT -> 'int16_t'
// handle : void* in/out -> 'void *'
// iRecord : SHORT -> 'int16_t'
// szSqlState : WORD* optional, out -> 'uint16_t *'
// pfNativeError : INT* in/out -> 'int32_t *'
// szErrorMsg : WORD* optional, out -> 'uint16_t *'
// cchErrorMsgMax : SHORT -> 'int16_t'
// pcchErrorMsg : SHORT* in/out -> 'int16_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("ODBC32.dll", {
  SQLGetDiagRecW: { parameters: ["i16", "pointer", "i16", "pointer", "pointer", "pointer", "i16", "pointer"], result: "i16" },
});
// lib.symbols.SQLGetDiagRecW(fHandleType, handle, iRecord, szSqlState, pfNativeError, szErrorMsg, cchErrorMsgMax, pcchErrorMsg)
// fHandleType : SHORT -> "i16"
// handle : void* in/out -> "pointer"
// iRecord : SHORT -> "i16"
// szSqlState : WORD* optional, out -> "pointer"
// pfNativeError : INT* in/out -> "pointer"
// szErrorMsg : WORD* optional, out -> "pointer"
// cchErrorMsgMax : SHORT -> "i16"
// pcchErrorMsg : SHORT* in/out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int16_t SQLGetDiagRecW(
    int16_t fHandleType,
    void* handle,
    int16_t iRecord,
    uint16_t* szSqlState,
    int32_t* pfNativeError,
    uint16_t* szErrorMsg,
    int16_t cchErrorMsgMax,
    int16_t* pcchErrorMsg);
C, "ODBC32.dll");
// $ffi->SQLGetDiagRecW(fHandleType, handle, iRecord, szSqlState, pfNativeError, szErrorMsg, cchErrorMsgMax, pcchErrorMsg);
// fHandleType : SHORT
// handle : void* in/out
// iRecord : SHORT
// szSqlState : WORD* optional, out
// pfNativeError : INT* in/out
// szErrorMsg : WORD* optional, out
// cchErrorMsgMax : SHORT
// pcchErrorMsg : SHORT* in/out
// 構造体/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 SQLGetDiagRecW(
        short fHandleType,   // SHORT
        Pointer handle,   // void* in/out
        short iRecord,   // SHORT
        ShortByReference szSqlState,   // WORD* optional, out
        IntByReference pfNativeError,   // INT* in/out
        ShortByReference szErrorMsg,   // WORD* optional, out
        short cchErrorMsgMax,   // SHORT
        ShortByReference pcchErrorMsg   // SHORT* in/out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("odbc32")]
lib LibODBC32
  fun SQLGetDiagRecW = SQLGetDiagRecW(
    fHandleType : Int16,   # SHORT
    handle : Void*,   # void* in/out
    iRecord : Int16,   # SHORT
    szSqlState : UInt16*,   # WORD* optional, out
    pfNativeError : Int32*,   # INT* in/out
    szErrorMsg : UInt16*,   # WORD* optional, out
    cchErrorMsgMax : Int16,   # SHORT
    pcchErrorMsg : Int16*   # SHORT* in/out
  ) : Int16
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef SQLGetDiagRecWNative = Int16 Function(Int16, Pointer<Void>, Int16, Pointer<Uint16>, Pointer<Int32>, Pointer<Uint16>, Int16, Pointer<Int16>);
typedef SQLGetDiagRecWDart = int Function(int, Pointer<Void>, int, Pointer<Uint16>, Pointer<Int32>, Pointer<Uint16>, int, Pointer<Int16>);
final SQLGetDiagRecW = DynamicLibrary.open('ODBC32.dll')
    .lookupFunction<SQLGetDiagRecWNative, SQLGetDiagRecWDart>('SQLGetDiagRecW');
// fHandleType : SHORT -> Int16
// handle : void* in/out -> Pointer<Void>
// iRecord : SHORT -> Int16
// szSqlState : WORD* optional, out -> Pointer<Uint16>
// pfNativeError : INT* in/out -> Pointer<Int32>
// szErrorMsg : WORD* optional, out -> Pointer<Uint16>
// cchErrorMsgMax : SHORT -> Int16
// pcchErrorMsg : SHORT* in/out -> Pointer<Int16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SQLGetDiagRecW(
  fHandleType: Smallint;   // SHORT
  handle: Pointer;   // void* in/out
  iRecord: Smallint;   // SHORT
  szSqlState: Pointer;   // WORD* optional, out
  pfNativeError: Pointer;   // INT* in/out
  szErrorMsg: Pointer;   // WORD* optional, out
  cchErrorMsgMax: Smallint;   // SHORT
  pcchErrorMsg: Pointer   // SHORT* in/out
): Smallint; stdcall;
  external 'ODBC32.dll' name 'SQLGetDiagRecW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SQLGetDiagRecW"
  c_SQLGetDiagRecW :: Int16 -> Ptr () -> Int16 -> Ptr Word16 -> Ptr Int32 -> Ptr Word16 -> Int16 -> Ptr Int16 -> IO Int16
-- fHandleType : SHORT -> Int16
-- handle : void* in/out -> Ptr ()
-- iRecord : SHORT -> Int16
-- szSqlState : WORD* optional, out -> Ptr Word16
-- pfNativeError : INT* in/out -> Ptr Int32
-- szErrorMsg : WORD* optional, out -> Ptr Word16
-- cchErrorMsgMax : SHORT -> Int16
-- pcchErrorMsg : SHORT* in/out -> Ptr Int16
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let sqlgetdiagrecw =
  foreign "SQLGetDiagRecW"
    (int16_t @-> (ptr void) @-> int16_t @-> (ptr uint16_t) @-> (ptr int32_t) @-> (ptr uint16_t) @-> int16_t @-> (ptr int16_t) @-> returning int16_t)
(* fHandleType : SHORT -> int16_t *)
(* handle : void* in/out -> (ptr void) *)
(* iRecord : SHORT -> int16_t *)
(* szSqlState : WORD* optional, out -> (ptr uint16_t) *)
(* pfNativeError : INT* in/out -> (ptr int32_t) *)
(* szErrorMsg : WORD* optional, out -> (ptr uint16_t) *)
(* cchErrorMsgMax : SHORT -> int16_t *)
(* pcchErrorMsg : SHORT* in/out -> (ptr int16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library odbc32 (t "ODBC32.dll"))
(cffi:use-foreign-library odbc32)

(cffi:defcfun ("SQLGetDiagRecW" sqlget-diag-rec-w :convention :stdcall) :int16
  (f-handle-type :int16)   ; SHORT
  (handle :pointer)   ; void* in/out
  (i-record :int16)   ; SHORT
  (sz-sql-state :pointer)   ; WORD* optional, out
  (pf-native-error :pointer)   ; INT* in/out
  (sz-error-msg :pointer)   ; WORD* optional, out
  (cch-error-msg-max :int16)   ; SHORT
  (pcch-error-msg :pointer))   ; SHORT* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SQLGetDiagRecW = Win32::API::More->new('ODBC32',
    'short SQLGetDiagRecW(short fHandleType, LPVOID handle, short iRecord, LPVOID szSqlState, LPVOID pfNativeError, LPVOID szErrorMsg, short cchErrorMsgMax, LPVOID pcchErrorMsg)');
# my $ret = $SQLGetDiagRecW->Call($fHandleType, $handle, $iRecord, $szSqlState, $pfNativeError, $szErrorMsg, $cchErrorMsgMax, $pcchErrorMsg);
# fHandleType : SHORT -> short
# handle : void* in/out -> LPVOID
# iRecord : SHORT -> short
# szSqlState : WORD* optional, out -> LPVOID
# pfNativeError : INT* in/out -> LPVOID
# szErrorMsg : WORD* optional, out -> LPVOID
# cchErrorMsgMax : SHORT -> short
# pcchErrorMsg : SHORT* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い