Calling Managed .NET C# COM Objects from Unmanaged C++

February 8th, 2010 Phanix

從 VC++ 中呼叫 C# 所寫的 COM 物件,主要的可以分成三項需要注意的事情。

1. 怎麼讓 C# class library 在編譯出 DLL 的同時同時產生 COM 物件
2. 怎麼讓系統認得這個這個 COM 物件
3. 怎麼在 VC++ 中用這個 COM 物件

這個網頁裡頭寫得非常清楚,同時還有範例下載。

[C#] Threading with Parameters / 帶參數的執行緒

December 10th, 2009 Phanix

主要有兩種方式,第一種為使用 ParameterizedThreadStart,但是這不是一個安全的方式。第二種方法是將要交給執行緒執行的方法與參數封裝到類別裡頭去,建立該類別的 Instance 之後就可以交給執行緒去執行,以下為 sample code… (另外要注意的是 Threading.Join() 的使用)

using System;
using System.Threading;

//ThreadWithState 類別裡包含執行任務的方法
public class ThreadWithState {
    //要用到的屬性,也就是我們要傳遞的參數
    private string boilerplate;
    private int value;

    //包含參數的 Constructor
    public ThreadWithState(string text, int number)
    {
        boilerplate = text;
        value = number;
    }

    //要丟給執行緒執行的方法,無 return value 就是為了能讓ThreadStart來呼叫
    public void ThreadProc()
    {
        //這裡就是要執行的任務, 只顯示一下傳入的參數
         Console.WriteLine(boilerplate, value);
    }
}

//Main entrance
public class Example {
    public static void Main()
    {
        //實例化ThreadWithState類別,為執行緒提供參數
        ThreadWithState tws = new ThreadWithState(
            "This report displays the number {0}.", 42);

        // 創建執行任務的執行緒,並執行
        Thread t = new Thread(new ThreadStart(tws.ThreadProc));
        t.Start();
        Console.WriteLine("Main thread does some work, then waits.");
        t.Join();
        Console.WriteLine(
            "Independent task has completed; main thread ends.");
    }
}

C# Wrapping Library of Google AJAX Search API

November 26th, 2009 Phanix

以前在程式裡頭用到 Google Search 的機會比較少,所以都是直接把 search term 丟給 Google search,然後 parsing 回傳 page 這樣土法硬幹的方式。

不過最近使用量比較大,所以就乾脆來找 Google Search API ,沒想到 Google 說現在一般的 Search API 已經不接受申請了,比較建議使用 AJAX API。

看了一下說明文件,對於要寫網頁程式的話確實蠻方便的,不過對於要寫 Windows / Console 程式來說就不是這麼好用… 「該不會要自己來寫一個 library 把這個 AJAX Search API 包起來吧?」

正當腦子裡才冒出這個念頭的時候,另外一個聲音響起,「不對,這事情一定也有人想過,然後很好心地做出來了」… 果不其然,找了一下就找到了 http://gapidotnet.codeplex.com/ 這個 wrapping api… 真是太棒了… XD

C# Make a window program in system tray, hidden in task bar, and hidden in alt-tab list

September 18th, 2009 Phanix

讓視窗程式只在 system tray 中顯示, 不會出現在工作列(task bar)中, 也不會出現在 alt-tab 清單裡頭

Read the rest of this entry / 繼續閱讀 »

C# mount / unmount UNC (network folder / drive) 連結網路磁碟機

September 8th, 2009 Phanix

有些時候為了 security 問題或者想要詳細紀錄使用者存取網路磁碟機或目錄中資料的情況, 所以會另外建立帳號並透過程式來存取網路磁碟機, 而不讓使用者直接用他的帳號密碼來存取, 這時候就需要讓程式可以建立網路磁碟機與中斷連線.

我自己比較喜歡第一個方式, 相對來說簡單許多.
Read the rest of this entry / 繼續閱讀 »

Visual Studio .Net 設定 DLL / EXE 版本編號

August 12th, 2009 Phanix

in Properties\AssemblyInfo.cs

[assembly: AssemblyVersion("1.0.0.1")]
[assembly: AssemblyFileVersion("1.0.0.1")]

C# UDP server sending and client receiving

August 10th, 2009 Phanix

UDP Server

private void ServerBroadcastThread()
{
    int GroupPort = 15000;

    UdpClient udp = new UdpClient();
    udp.EnableBroadcast = true;
    IPEndPoint groupEP = new IPEndPoint(IPAddress.Broadcast, GroupPort);

    string str4;
    byte[] sendBytes4;

    while (true)
    {
        str4 = "Is anyone out there?";
        sendBytes4 = Encoding.Default.GetBytes(str4);
        udp.Send(sendBytes4, sendBytes4.Length, groupEP);

        Thread.Sleep(10000);
    }
}

UDP Client

private void ClientBroadcastThread()
{
    UdpClient receivingUdpClient = new UdpClient(15000);
    IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

    string returnData;
    byte[] receiveBytes;

    while (true)
    {
        try
        {
            // Blocks until a message returns on this socket from a remote host.
            receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);

            returnData = Encoding.ASCII.GetString(receiveBytes);

            MessageBox.Show(returnData);
        }
        catch (Exception e)
        {
            //Console.WriteLine(e.ToString());
        }
    }
}

Add description to functions and parameters in .NET

August 7th, 2009 Phanix

While using some library functions in Visual Studio .NET, you can always find such kind of descriptions which help you to get some idea of parameters and functions.

csharp_function_description (by Phanix)

However, how to provide such kind of descriptions in your class libraries?

Plz refer this! http://www.codeproject.com/KB/XML/csharpcodedocumentation.aspx?msg=1643354

[C#, .NET] Run a program in tray (minimized window and no-shown in taskbar) with right clicked pop-up menu

July 30th, 2009 Phanix

程式執行後以最小化的方式執行,即不會出現在工作列上,而會出現在tray裡頭。而在 tray icon 上可以點滑鼠右鍵來彈出功能表單(menu),進行各功能選擇(如結束等)。

Read the rest of this entry / 繼續閱讀 »

.net 無視窗背景執行的程式

July 15th, 2009 Phanix

有時候為了要偷偷做一些事情, 所以會想要讓程式以無視窗在背景執行, 而要做出這樣的程式其實方法很簡單

  1. 在建立專案的時候選擇「主控台應用程式」(console application)
  2. 設定專案屬性(properties), 將輸出類型改為「windows 應用程式」

這樣就可以了, 當發現到這樣做就搞定的時候還有點訝異怎麼這麼容易…