C#调用WinAPI

windxx 2010-12-21 04:59:20
请问比如我想调用如下的WinAPI
GetLogicalDriveStrings(DWORD nBufferLength,LPTSTR lpBuffer);
我看GetLogicalDriveStrings的解释,如果调用成功,会返回驱动器名称的若干个字符串,我在C#中进行如下调用:

[DllImport("Kernel32")]
public static extern int GetLogicalDriveStrings(int nBufferLength, string lpBuffer);
string str = "";
MessageBox.Show(GetLogicalDriveStrings(125, str).ToString());
MessageBox.Show(str);

结果GetLogicalDriveStrings返回32,str = "",应该是调用有问题,请问有人知道正确的调用方法么?谢谢
...全文
102 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
fongye365 2010-12-21
  • 打赏
  • 举报
回复
$('#btn_complete').click(
function() {
if ($("#defaultConfigForm").validate().form()) {
$('#btn_complete').attr( {
"disabled" : "disabled"
});
blockUI();
$.post('${ctx}/init/setConfig.do', $('#defaultConfigForm')
.serializeArray(), function(data) {
if (data == 'success') {
window.location = "${ctx}/security/login.do";
} else {
unBlockUI();
alert('系统初始化失败!');
$('#btn_complete').removeAttr("disabled");
}
});
}
});
  • 打赏
  • 举报
回复
StringBuilder sb = new StringBuilder(256);//指定容量
GetLogicalDriveStrings(sb.Capacity, sb);
MessageBox.Show(sb.ToString());

这是MSDN推荐的标准方法
windxx 2010-12-21
  • 打赏
  • 举报
回复
谢谢,得到结果了。
ivorstar 2010-12-21
  • 打赏
  • 举报
回复
没有返回0,说明调用成功,试试out string lpBuffer
或者参考
http://www.pinvoke.net/search.aspx?search=GetLogicalDriveStrings&namespace=[All]

// static extern uint GetLogicalDriveStrings(uint nBufferLength,
static extern uint GetLogicalDriveStrings(uint nBufferLength, [Out] char[] lpBuffer);
static extern uint GetLogicalDriveStrings(uint nBufferLength, [Out] char[] lpBuffer);
uint code = GetLogicalDriveStrings(size, buffer);

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧