Question

J'ai un objet COM que j'essaie d'envelopper dans une classe C # afin de le rendre plus facilement disponible pour d'autres applications qui souhaitent la consommer.

J'ai le code suivant qui crée une instance de l'objet COM, puis à l'aide de la réflexion, appelle une méthode pour récupérer des données utilisateur. Ce code fonctionne bien lorsqu'il est situé dans une page ASPX.

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

Toutefois, lorsque je déplace le code dans un fichier de classe (jd_api.cs) afin de le résumer à partir du site Web réel, je ne peux plus le faire fonctionner. Par exemple, j'ai la méthode statique suivante qui est déclarée comme telle:

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;
}

Malheureusement, l'objet Server est limité à certaines bibliothèques ASP.NET incluses par défaut dans les applications Web, et le code ci-dessus était donc de non-go. Donc, à ce stade, j'ai décidé d'essayer de créer une instance de l'objet COM comme tel:

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;
}

Cependant, au moment de l'exécution, je reçois une erreur indiquant: " a tenté de lire ou d'écrire une mémoire protégée. Cela indique souvent que d'autres mémoires sont corrompues. ".

Je ne suis pas sûr où aller d'ici. Toute aide sur la manière de résumer la création d'une instance de cet objet COM à une couche qui ne se trouve pas dans l'application Web elle-même serait grandement appréciée. Merci !!

Était-ce utile?

La solution

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.

Autres conseils

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!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top