[英]Running Powershell script causes 'Memory corruption' or 'Cannot Read or Write'
我正在制作一個程序,為您制作 Wi-Fi 熱點並使用 powershell 腳本自動啟用 Internet 連接共享。
該腳本可以正常工作並完美運行,但我必須等待它完成才能通知用戶它已完成。 我使用下面的代碼,但有效...
但它崩潰並導致我的家用計算機出現錯誤,分配速度更快。 我收到無法讀取或寫入或內存損壞的錯誤,我無法真正解釋。
public static void ToggleIcs(string connectionInterface, bool state)
{
string toggle;
string par1;
string par2;
if (state){
toggle = "EnableSharing";
par1 = "0";
par2 = "1";
}else{
toggle = "DisableSharing";
par1 = "";
par2 = "";
}
using (PowerShell powerShellInstance = PowerShell.Create())
{
// this script enables or disables internet sharing with the connectionInterface given.
powerShellInstance.AddScript("" +
"regsvr32 hnetcfg.dll /s;" +
"$m = New-Object -ComObject HNetCfg.HNetShare;" +
"$m.EnumEveryConnection |% { $m.NetConnectionProps.Invoke($_) };" +
"$c = $m.EnumEveryConnection |? { $m.NetConnectionProps.Invoke($_).Name -eq '" + connectionInterface + "' };" +
"$config = $m.INetSharingConfigurationForINetConnection.Invoke($c);" +
"Write-Output $config.SharingEnabled;" +
"Write-Output $config.SharingConnectionType;" +
"$config." + toggle + "(" + par1 + ");" +
"$m2 = New-Object -ComObject HNetCfg.HNetShare;" +
"$m2.EnumEveryConnection |% { $m2.NetConnectionProps.Invoke($_) };" +
"$c2 = $m2.EnumEveryConnection |? { $m2.NetConnectionProps.Invoke($_).DeviceName -Match 'Microsoft Hosted Network Virtual Adapter' };" +
"$config2 = $m2.INetSharingConfigurationForINetConnection.Invoke($c2);" +
"Write-Output $config2.SharingEnabled;" +
"Write-Output $config2.SharingConnectionType;" +
"$config." + toggle + "(" + par2 + ");");
PSDataCollection<PSObject> outputCollection = new PSDataCollection<PSObject>();
IAsyncResult result = powerShellInstance.BeginInvoke<PSObject, PSObject>(null, outputCollection);
Console.WriteLine(DateTime.Now + ">> Started Powershell script");
int i = 0;
while (!result.IsCompleted)
{
if (i < 60)
{
Console.WriteLine(DateTime.Now + ">> Running script");
}
else
{
ScriptFailed();
powerShellInstance.Stop();
break;
}
i++;
Thread.Sleep(1000);
}
Console.WriteLine(DateTime.Now + ">> Executed Internet Sharing script");
}
}
這是我得到的錯誤:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
我沒有得到一條說明它在哪里崩潰但它在Thread.Sleep(1000);
之后崩潰的行Thread.Sleep(1000);
. 我發現使用斷點
正如我所說,該腳本在我的筆記本電腦上運行良好,並且在我更快的計算機上也能正常運行,但是一旦遇到Thread.Sleep(1000)
就會崩潰。 在它崩潰后,我檢查了網絡和共享中心是否啟用了 ICS,並且它正確地啟用了。
我嘗試刪除Thread.Sleep(1000);
但無論如何它都會在前一行崩潰。
我可以嘗試或以不同的方式做些什么?
我還沒有堆棧跟蹤,因為我不在我的更快的 PC 上它崩潰了。 但我會盡快發布。
正如 TheLethalCoder 所提到的,可能是我在更新時嘗試訪問IsCompleted
。 如果這就是它發生的原因,我將如何檢查它是否被更改或等待它完成。
編輯
因為我真的不知道調用堆棧是做什么的,或者堆棧跟蹤是什么,以及我如何使用它來提供我在崩潰前一刻看到的圖像。
編輯我做了一些窺探,發現了一些我嘗試過的東西。
首先,在應用程序屬性 -> 構建中,我勾選了“首選 32 位”,因為有些人用這個解決了他們的問題。 我沒有崩潰,但腳本也無法運行並導致我的互聯網連接中斷。
所以我把它重新打開。 我還嘗試了netsh winsock reset
命令並重新啟動了我的電腦,但它仍然崩潰了。
我現在已經沒有線索了,但是我為那些尋求答案的人發布了這兩件事,也許這會奏效。
MSDN 文章:輪詢異步操作的狀態指出您輪詢結果的方式是正確的。 但是他們的例子不包括Thread.Sleep
:
while (result.IsCompleted != true) { UpdateUserInterface(); }
因此,為了消除這種情況,我將使用以下代碼片段在不休眠的情況下跟蹤變量,請注意,盡管在每個循環中打印到Console
會快速污染窗口:
Stopwatch sw = new Stopwatch();
sw.Start();
while (!result.IsCompleted)
{
if (sw.EllapsedMilliseconds >= 60000) //60 seconds
{
break;
}
Console.WriteLine(DateTime.Now + ">> Running script");
}
if (!result.IsCompleted)
{
ScriptFailed();
powerShellInstance.Stop();
}
這與其說是答案,不如說是評論,但是作為評論使用時間太長了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.