顯示具有 .Net Framework程式設計 標籤的文章。 顯示所有文章
顯示具有 .Net Framework程式設計 標籤的文章。 顯示所有文章

2009年3月25日 星期三

ASP.NET權限不足,無法使用WINDOWS應用程式

最近遇到了個問題
撰寫了一個web service掛載於IIS下,web service會透過SKYPE API呼叫本機電腦裡面的應用程式(SKYPE)作發送簡訊的功能。

未發行至IIS下叫用都是正常的,也寫了一個windows form 來叫用確認是可以執行的
但一發布到IIS下,偵錯時卻發生了.. client端程式 出現wait timeout..

開啟web service 偵錯模式,再來看看web service怎說~
web service先參考skype4com.dll,在程式裡new skypeclass()實體為 myskype都OK,下一步
myskype.sendSMS("+886xxxxxxxxx","hello","") 馬上消失,就像正在處理工作一樣程式碼沒有反紅,看不到偵錯情形。
利用try and catch把runtime error抓出來,錯誤顯示
System.Runtime.InteropServices.COMException (0x80040200): Not attached.
於 SKYPE4COMLib.SkypeClass.SendSms(String TargetNumbers, String MessageText, String ReplyToNumber)
於 Service.HelloWorld() 於 c:\Inetpub\wwwroot\WSsendSKYPE2\App_Code\Service.cs: 行 70

怪哉~GOOGLE了很久,沒有一個解決的方法~
問題可能出在"安全性"問題上~
ASP.NET預設使用者使用資源的安全性與windows應用程式預設安全性不同,所以才會有掛載在IIS下的webservice與windows form使用SKYPE.EXE權限不同!

MSDN tech提到安全性原則部分
有兩個步驟:
1.讓ASP.NET使用者權限提到最高,可以執行、讀取、寫入等等
2.讓SKYPE檔案降低權限可以被設定的使用者群組執行、讀取等

之後再在web service程式碼裡頭加上
System.Diagnostics.Process.Start("Skype.exe"); 允許讀取到此執行檔


System.Diagnostics.Process.Start("Skype.exe");
錯誤顯示 :
{"System.Web.Services.Protocols.SoapException: 伺服器無法處理要求。 ---> System.ComponentModel.Win32Exception: 系統找不到指定的檔案。\n 於 System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)\n 於 System.Diagnostics.Process.Start()\n 於 System.Diagnostics.Process.Start(ProcessStartInfo startInfo)\n 於 System.Diagnostics.Process.Start(String fileName)\n 於 Service..ctor() 於 c:\\Inetpub\\wwwroot\\WSsendSKYPE2\\App_Code\\Service.cs: 行 15\n --- 內部例外狀況堆疊追蹤的結尾 ---"}

挖哩,還是讀取不到~>~<~ 不知道是使用者群組原則是否沒有設定好,但我已經把權限都改成"完全控制"了呀?! 之後也試過了web.config 組態設定,還沒試"註冊表"修改~畢竟如果轉移網站的話,不好意思把別人的系統弄掛吧>< 如果web service 可以叫用額外寫的windows form(APP1),而APP1可以使用SKYPE API進行發送簡訊,這樣也許可以完成。只要APP1的路徑掛在IIS下,但不成為ASP.net的成員。不知道這樣的觀念有沒有錯~ 終於找到了這個想法的實現方式~"遠端物件"~~~~~ 利用.net framwork 提供的 remoting namespace把windows application當作遠端物件使用~ 依然要考慮到使用者群組安全性原則~ 但是..這又是一個大議題..依目前程度要花不少時間來看。 想著這個方法的可行性入睡~~~ 也許有另一個方式會比較間接,但可行性有9成!! 利用資料庫當作媒介,讓WEB SERVICE作寫入,另一隻程式做讀取判斷來執行SKYPE 一樣另外寫一個windows application,這隻程式不斷讀取資料庫作判斷。 這樣一來,windows application是在系統管理者的權限下執行而可以使用SKYPE API 再者,移動網站時,也不需要考慮到路徑的問題,只需要多安裝這個application了~ 但,這畢竟是較低級的方法,application 會不斷使用系統資源,還是使用event觸發的方式較能節省系統資源。

2009年3月14日 星期六

開啟關閉 windows mobile WIFI

1.
[DllImport("coredll.dll")]
public static extern int DevicePowerNotify(string device, CEDEVICE_POWER_STATE state, int flags);


[DllImport("coredll.dll")]
public static extern int SetDevicePower(string pvDevice, int df, CEDEVICE_POWER_STATE ds);


没有专门针对WIFI设备的API, 但是可以通过上面两个方法,控制WIFI状态

2. ref: http://topic.csdn.net/u/20080728/20/a5efe62a-629c-4811-add5-bc6c53fcdd5b.html
用无线管理器ChangeRadioState函数可以指定打开或关闭某一设备,包括WIFI


首先使用函数HRESULT GetWirelessDevices()获得一个设备链表,在设备链表中根据设备类型,找到wifi设备,然后调用ChangeRadioState()打开或者关闭指定的设备。
有关上述函数的详细使用,可以查windows mobile 6 documentation

GetWirelessDevices(),ChangeRadioState()都是MS公布给OEM的标准接口,WM6都是支持的。

openNETCF有,且有例子,你找找去。


看看人家做的
http://www.cnblogs.com/MS-Frank/archive/2008/11/14/1333664.html

軟體 wifiswitch
http://www.winbile.net/bbs/forums/threads/1014040.aspx
http://www.pudn.com/downloads90/sourcecode/windows/comm/detail342414.html

Native Wifi api
该api的可以实现windows的无线客户端的网络参数配置(WlanSetProfile),连接指定的ap,断开连接和获得其他关于无线网络的信息(ssid,rssi等)。

並且有一個斷開WIFI的範例。

另一個繼續深入的搜尋資源
http://search.pudn.com/search.asp?keyword=%20wifi


coredll 全部的函式名稱
http://topic.csdn.net/t/20050826/13/4233161.html

2009年3月8日 星期日

開啟/關閉 WIFI 裝置

問題
http://topic.csdn.net/u/20080728/20/a5efe62a-629c-4811-add5-bc6c53fcdd5b.html

解法

Windows Mobile 中开关 WIFI 的“通用”代码

SystemState.WiFiStatePowerOn Property  讀取WIFI狀態的屬性,但無法設置開關
英文的 ><

Deactivate /Activate all Services like (Internet,WIFI,Bluetooth,WLAN..)

Enabling Radio in the Windows Mobile 5.0

WiFi Power On Event

開啟D0 關閉D4

We can implement this functionality with ease. Hope this can give your some idea.

See the code below:

// D0 indicates open; D4 indicates close;

CEDEVICE_POWER_STATE state = D0;

HRESULT hr = SetDevicePower(TEXT(""),POWER_NAME,state);

Note: We use SetDevicePower function here. It sets the device power state for a device.

  1. 1st parameter: specify the device.
  2. 2nd parameter: we use POWER_NAME here to indicate that the first parameter is just a device name.
  3. 3rd parameter: sets the power state.

In order to get information in details, you can find it in MSDN. Thanks!

Regards,

Zero Dai - MSFT



之後
Battery Status

GPS Application

Mobile GPS

2009年2月18日 星期三

scoket傳送封包流過長

趁著從台北開會搭公車回來,測試研究的系統穩定度
發現搭車到到一半時就發生過長時間才收到server的回應
最後一筆有效紀錄(經web server過濾後所顯示)的地圖標點

都來沒走一半呢...
回到研究室看看debug訊息~

怪哉~ 再看console
不知道什麼原因造成多筆記錄在同一筆傳送
socket封包支援到1024byte,一旦超過,傳送的data stream 就會被截斷在下一個封包繼續傳送
如果網路狀況不擁擠(小於1024byte),那麼頂多只會loss掉幾筆資料..
但如果遇到split(,),剛好data資料開頭剛好是"," 程式裡頭判斷所欲存之值為空..就會發生錯誤(格式不對)

看看資料庫就知道...多糟了><

看樣子,要避開這種情況目前想到只有
  1. 修改每次傳送封包格式的大小,能整除1024(目前一筆資料大小為76byte以下),這樣就能確保所存資料格式正確
    但server程式部分要限制每個欄位的資料大小,之後如果PDA要新增傳送資料,要再修改 重算能整除值...而對定位而言,更會遺失至多12筆資料(1024/76=13.47)
  2. server程式先行判斷所收到資料筆數(依封包大小),再複雜變數儲存判斷,但好像遇到封包過大,還是有stream被截的問題...
  3. 搜尋stream buffer相關.. 迫使程式接收一定是一筆一筆進來分析.
看樣子 方法三應該是比較簡單的,不用修改太多程式碼,要花時間的是找stream buffer部分的知識了~

2009年1月16日 星期五

ThreadPool..::.QueueUserWorkItem 方法 (WaitCallback, Object)

參考 MSDN  http://msdn.microsoft.com/zh-tw/library/4yd16hza.aspx

// This example shows how to create an object containing task

// information, and pass that object to a task queued for
// execution by the thread pool.
using System;
using System.Threading;

// TaskInfo holds state information for a task that will be
// executed by a ThreadPool thread.
public class TaskInfo {
// State information for the task. These members
// can be implemented as read-only properties, read/write
// properties with validation, and so on, as required.
public string Boilerplate;
public int Value;

// Public constructor provides an easy way to supply all
// the information needed for the task.
public TaskInfo(string text, int number) {
Boilerplate = text;
Value = number;
}
}

public class Example {
public static void Main() {
// Create an object containing the information needed
// for the task.
TaskInfo ti = new TaskInfo("This report displays the number {0}.", 42);

// Queue the task and data.
if (ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), ti)) {
Console.WriteLine("Main thread does some work, then sleeps.");

// If you comment out the Sleep, the main thread exits before
// the ThreadPool task has a chance to run. ThreadPool uses
// background threads, which do not keep the application
// running. (This is a simple example of a race condition.)
Thread.Sleep(1000);

Console.WriteLine("Main thread exits.");
}
else {
Console.WriteLine("Unable to queue ThreadPool request.");
}
}

// The thread procedure performs the independent task, in this case
// formatting and printing a very simple report.
//
static void ThreadProc(Object stateInfo) {
TaskInfo ti = (TaskInfo) stateInfo;
Console.WriteLine(ti.Boilerplate, ti.Value);
}
}
///////////////////////
當執行此敘述 if (ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), ti
ti會一起傳送到 ThreadProc(Object stateInfo) 裡面的
stateInfo
因此 在
ThreadProc(Object stateInfo)中 需要轉換 Object 為 TaskInfo 型態再轉存一次

2009年1月12日 星期一

淺談多執行緒

參考

C# 隨性筆記


淺談多執行緒(一) --多執行緒介紹:定義造物件,與啟動

淺談多執行緒(二) --多執行緒共用資料的碰撞問題與LOCK

淺談多執行緒(三) --多執行緒死結deadlock 與使用Monitor.TryEnter方法鎖定

補充:

1.參考MSDN
  • 建立和執行執行緒
  • 執行緒的同步化
  • 執行緒間的互動
  • 使用執行緒集區
  • 使用 Mutex 物件來保護共用的資源
http://msdn.microsoft.com/zh-tw/library/aa288472(VS.71).aspx

2.用於非同步的
ManualResetEvent
http://www.webjx.com/htmldata/2006-08-14/1155517019.html