如何在C#中使用SQLInstallDriverEx? [英] How to use SQLInstallDriverEx in C#?

查看:69
本文介绍了如何在C#中使用SQLInstallDriverEx?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何重写以下函数以便在C#中使用它?该功能是odbccp32.dll的一部分,用于安装ODBC driver.

How would I rewrite the following function in order to use it in C#? The function is part of odbccp32.dll and is used to install an ODBC driver.

BOOL SQLInstallDriverEx(  
     LPCSTR    lpszDriver,  
     LPCSTR    lpszPathIn,  
     LPSTR     lpszPathOut,  
     WORD      cbPathOutMax,  
     WORD *    pcbPathOut,  
     WORD      fRequest,  
     LPDWORD   lpdwUsageCount);

SQLInstallDriverEx参考

这是我的尝试:

 [DllImport("odbccp32")]
 private static extern bool SQLInstallDriverEx(
      string lpszDriver,
      string lpszPathIn,
      out string lpszPathOut,
      ushort cbPathOutMax,
      out uint pcbPathOut,
      ushort fRequest,
      out uint lpdwUsageCount);

示例用法也将深深地感激.

A sample usage would be deeply appreciated as well.

推荐答案

p/Invoke Toolkit生成以下内容:

p/Invoke Toolkit generates the following:

[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用于输出"字符串.您需要调用该方法两次,一次用于解析路径/路径长度,第二次用于传递这些值.如果您事先知道这些值,则可以跳过该步骤,但是路径可以从如果驱动程序已存在或路径太长,则提供的内容.第二个调用要求lpszPathOut为非空且cbPathOutMax为非零.

The In/MarshalAs attributes may not be necessary. StringBuilders work for "out" strings as the marshaller knows how to convert under the hood. You need to call the method twice, once to resolve the path/path length and the second to pass those values in. That step can be skipped if you know these before hand, but the path can change from what you provide if the driver already exists or the path is too long. The second call requires that the lpszPathOut be non-null and cbPathOutMax be non-zero.

您传递给该方法的驱动程序描述由一个空字节终止的字符串列表组成.该列表以双null终止.

The driver description that you pass to the method is made up of a list of null-byte terminated strings. The list is terminated with a 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方法.幸运的是,该方法有一个ap/invoke页面,以及如何在 http上调用它的页面://www.pinvoke.net/default.aspx/odbccp32/SQLInstallerError.html .您链接的MSDN页面上已经概述了错误代码及其含义.

If the return value is false, you will need to call the SqlInstallerError method which you will have to import as well. Fortunately there is a p/invoke page for this method and how to call it available at http://www.pinvoke.net/default.aspx/odbccp32/SQLInstallerError.html. The error codes and what they mean are already outlined on the MSDN page you linked.

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

Note that you can skip the two-phase method call and call with ODBC_INSTALL_COMPLETE once, provided you have provided correct buffer/sizes.

这篇关于如何在C#中使用SQLInstallDriverEx?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆