如何在C#中使用SQLInstallDriverEx?

时间:2017-03-08 23:18:36

标签: c# interop

如何在ODBC driver中重写以下函数?该函数是BOOL SQLInstallDriverEx( LPCSTR lpszDriver, LPCSTR lpszPathIn, LPSTR lpszPathOut, WORD cbPathOutMax, WORD * pcbPathOut, WORD fRequest, LPDWORD lpdwUsageCount); 的一部分,用于安装 [DllImport("odbccp32")] private static extern bool SQLInstallDriverEx( string lpszDriver, string lpszPathIn, out string lpszPathOut, ushort cbPathOutMax, out uint pcbPathOut, ushort fRequest, out uint lpdwUsageCount);

trainSet = numpy.reshape(trainSet, (trainSet.shape[0], 1, trainSet.shape[1]))

SQLInstallDriverEx Reference

这是我的尝试:

print(trainSet[0][0][0])
print(trainSet[1][0][1])

样本用法也会受到高度赞赏。

1 个答案:

答案 0 :(得分:2)

p / Invoke Toolkit生成以下内容:

[DllImport("odbccp32.dll")]
public static extern  bool SQLInstallDriverEx(
     [In, MarshalAs(UnmanagedType.LPStr)] string lpszDriver, 
     [In, MarshalAs(UnmanagedType.LPStr)] string lpszPathIn,
     [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpszPathOut, 
     ushort cbPathOutMax, ref ushort pcbPathOut, ushort fRequest, ref uint lpdwUsageCount);

public const ushort ODBC_INSTALL_INQUIRY = 1;
public const ushort ODBC_INSTALL_COMPLETE = 2;

In/MarshalAs属性可能没有必要。 StringBuilder为“out”字符串工作,因为编组人员知道如何在引擎盖下进行转换。您需要调用该方法两次,一次调用路径/路径长度,第二次调用这些值。如果您事先知道这些,则可以跳过该步骤,但路径可以从如果驱动程序已存在或路径太长,您提供的是什么。第二个调用要求lpszPathOut为非空且cbPathOutMax为非零。

传递给方法的驱动程序描述由空字节终止字符串列表组成。该列表以double null结尾。

// if target path is null, it should default to the system directory
var targetPath = @"c:\windows\system32";
ushort len = 0; uint usageCount=0;

// replace this with your actual driver description, note the double \0 at the end
var driver = "Text\0Driver=TEXT.DLL\0Setup=TXTSETUP.DLL\0FileUsage=1\0"
           + "FileExtns=*.txt,*.csv\0\0";

var ret = SQLInstallDriverEx(driver, targetPath, null, 0, 
                 ref len, ODBC_INSTALL_INQUIRY, ref usageCount);
if (ret) {
    var sbPath = new StringBuilder(len);
    if (ret = SQLInstallDriverEx(driver, targetPath, sbPath, len, 
                 ref len, ODBC_INSTALL_COMPLETE, ref usageCount)) {

        // sbPath will now contain the actual path, which may be different 
        // if there is a previous installation or the path was truncated
        // perhaps check here if that was intended

        // usageCount will be updated by the system
   }
}

if (!ret) {
    // query SqlInstallerError(...) here
}

如果返回值为false,则需要调用必须导入的SqlInstallerError方法。幸运的是,此方法有一个p / invoke页面,以及如何在http://www.pinvoke.net/default.aspx/odbccp32/SQLInstallerError.html处调用它。您链接的MSDN页面上已经列出了错误代码及其含义。

请注意,如果您提供了正确的缓冲区/大小,则可以跳过两阶段方法调用并使用ODBC_INSTALL_COMPLETE调用一次。