powershell能调用C++的函数吗


PowerShell交流中心分类: Powershell基础powershell能调用C++的函数吗
0
DJ asked 9 年 ago

我想使用GetPrivateProfileString函数来操作ini文件,但是这是一个c++的函数,我不知道powershell是否能调用C++的函数,如果可以需要怎么调用

0 Answers
1
Mooser Lee 管理员 answered 9 年 ago

先写C# ,然后用PowerShell调用C#中的方法:
 
[DllImport(“kernel32.dll”)]
private static extern int GetPrivateProfileSection(string lpAppName, byte[] lpszReturnBuffer, int nSize, string lpFileName);
private List<string> GetKeys(string iniFile, string category)
{
byte[] buffer = new byte[2048];
GetPrivateProfileSection(category, buffer, 2048, iniFile);
String[] tmp = Encoding.ASCII.GetString(buffer).Trim(‘\0’).Split(‘\0’);
List<string> result = new List<string>();
foreach (String entry in tmp)
{
result.Add(entry.Substring(0, entry.IndexOf(“=”)));
}
return result;
}
引用自:
Read all ini file values with GetPrivateProfileString

DJ replied 9 年 ago

不好意思,我没太明白先写C#是什么意思

Mooser Lee 管理员 replied 9 年 ago

已更新

DJ replied 9 年 ago

你是意思是让我先把这个函数在C#里实现,然后再封装成dll,之后再powershell里调用这个dll,是吗?

Mooser Lee 管理员 replied 9 年 ago

不需要封装成dll,PowerShell中可以直接编译和引用C#的。可以参考我之前的例子:调用系统函数锁屏:PowerShell 锁屏 http://www.pstips.net/lock-workstation.html

DJ replied 9 年 ago

好的,谢谢,我试一下