質問

私はそれを消費したい他のアプリケーションでもっと使えるようにするために、C#クラスで折り返しようとしているCOMオブジェクトを持っています。

COMオブジェクトのインスタンスを作成し、反射を使用すると、ユーザーデータを取得するメソッドを呼び出します。このコードは、ASPXページにあるときにうまく機能します。

object jdObj = Server.CreateObject("jd_api.UserCookie");
string username = jdObj.GetType().InvokeMember("GetUserName", System.Reflection.BindingFlags.InvokeMethod, null, jdObj , null).ToString();
.

しかし、実際のWebサイトから抽象化するためにコードをクラスファイル(JD_API.CS)に移動すると、もう機能することはできません。たとえば、次のような静的メソッドを使用しています。

public static string GetUserName() {

    object jdObj = Server.CreateObject("jd_api.UserCookie");
    string username = jdObj.GetType().InvokeMember("GetUserName",
System.Reflection.BindingFlags.InvokeMethod, null, jdObj , null).ToString();

    return username;
}
.

残念ながら、サーバーオブジェクトは、Webアプリケーションでデフォルトで含まれているASP.NETライブラリに制限されているため、上記のコードは無限でした。だからこの時点で私はそのようなcomオブジェクトのインスタンスを作成しようとしました:

public static string GetUserName() {

    Type type = Type.GetTypeFromProgID("jd_api.UserCookie");
    object jdObj = Activator.CreateInstance(type);

    string username = jdObj.GetType().InvokeMember("GetUserName", System.Reflection.BindingFlags.InvokeMethod, null, jdObj , null).ToString();

    return username;
}
.

しかし、実行時には、「は、保護メモリの読み書きまたは書き込みを試みたエラーを得ます。これは、他のメモリが破損しているという表示です。 "。

ここからどこへ行ったのかわからない。このCOMオブジェクトのインスタンスをWebアプリケーション自体の中に含まれていないレイヤーに抽象化する方法についての助けは大いに認められます。ありがとう!!

役に立ちましたか?

解決

Declare DLL functions within a class. Then define a static method for each DLL function you want to call. The following code sample creates a wrapper named Win32MessageBox that calls the MessageBox function in User32.dll each time a .NET app calls the object Show method. It requeres the System.Runtime.InteropServices namespace.

using System;
using System.Runtime.InteropServices;

class Win32MessageBox
{
    [DllImport("user32.dll")]
    private static extern int MessageBox(IntPtr hWnd, String text,
        String caption, uint type);

    public static void Show(string message, string caption)
    {
        MessageBox(new IntPtr(0), message, caption, 0);
    }
}

To call it, just type:

Win32MessageBox.Show("StackOverflow!", "my stack box");

The method where you call the above line doesn't need to be aware that it's a actually calling a function in an unmanaged DLL.

Resources: the MCTS Self-Paced Training Kit (Exam 70-536) by Tony Northrup.

他のヒント

Hove you tried usinsing interoperating

I've done the following in the past (working from memory so you might need to fiddle with this a bit):

  1. Right Click "References" in your project
  2. Select "Add Reference"
  3. Selelct the "Com" Tab
  4. Find and add your Com Instnace

In your class file

using yourComName;

public static string GetUserName() 
{
        yourComName.yourComClass jdObj = new  yourComClass();
        string username = jdObj.GetUserName(someParameters);
        return username;
}

Hope this a) works and b) helps!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top