Come consumare l'oggetto COM nel file di classe ASP.NET
-
14-11-2019 - |
Domanda
Ho un oggetto COM che sto cercando di avvolgere in una classe C # per renderlo più facilmente disponibile per altre applicazioni che desiderano consumarla.
Ho il seguente codice che crea un'istanza dell'oggetto COM, quindi l'utilizzo della riflessione effettua una chiamata a un metodo per recuperare i dati dell'utente. Questo codice funziona bene quando si trova in una pagina ASPX.
object jdObj = Server.CreateObject("jd_api.UserCookie");
string username = jdObj.GetType().InvokeMember("GetUserName", System.Reflection.BindingFlags.InvokeMethod, null, jdObj , null).ToString();
.
Tuttavia, quando spostando il codice in un file di classe (JD_API.CS) per astrarre dal sito web effettivo, non posso più farlo funzionare. Ad esempio, ho il seguente metodo statico che viene dichiarato come tale:
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;
}
.
Sfortunatamente, l'oggetto Server è limitato a alcune librerie ASP.NET incluse per impostazione predefinita nelle applicazioni Web, e quindi il codice sopra è stato un no-go. Quindi a questo punto ho deciso di provare a creare un'istanza dell'oggetto COM come tale:
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;
}
.
Tuttavia in runtime ottengo un errore che dice: " ha tentato di leggere o scrivere memoria protetta. Questo è spesso un'indicazione che l'altra memoria è corrotta. ". ". ". ". ".
Non sono sicuro di dove andare da qui. Qualsiasi aiuto su come astratto Creazione di un'istanza di questo oggetto COM a uno strato che non è all'interno dell'applicazione Web stesso sarebbe molto apprezzato. Grazie !!
Soluzione
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.
Altri suggerimenti
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):
- Right Click "References" in your project
- Select "Add Reference"
- Selelct the "Com" Tab
- 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!