如何消耗ASP.NET类文件COM对象 [英] How to consume COM object in ASP.NET class file

查看:178
本文介绍了如何消耗ASP.NET类文件COM对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我试图在C#类来包装,以使其更容易获得用于那些希望使用它的其他应用程序的COM对象。

I have a COM object that I am trying to wrap in a C# class in order to make it more readily available for other applications that wish to consume it.

我有以下的code创建的COM对象的实例,然后使用反射使得对方法的调用来检索用户数据。当它坐落在一个aspx页面此code正常工作。

I have the following code that creates an instance of the COM object, and then using reflection makes a call to a method to retrieve user data. This code works fine when it is located in an aspx page.

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

然而,当我为了它的实际网站移动code到A类文件(JD_API.cs)到抽象,我再也无法得到它的工作。例如,我有一个声明,例如像下面的静态方法:

However, when I move the code to a class file (JD_API.cs) in order to abstract it from the actual website, I can no longer get it to work. For example, I have the following static method that is declared like such:

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库,因此上述code是一个没有去。所以在这一点上,我决定尝试创建一个像这样的COM对象的实例:

Unfortunately, the Server object is restricted to some ASP.NET libraries that are included by default in web applications, and so the above code was a no-go. So at this point I decided to try to create an instance of the COM object like such:

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应用程序本身将大大AP preciated中的某一层任何帮助。谢谢!

I'm not sure where to go from here. Any help on how to abstract creating an instance of this COM object to a layer that is not within the web application itself would greatly appreciated. Thanks!!

推荐答案

声明一个类中的DLL函数。然后定义你想调用每个DLL函数的静态方法。
下面code样品创建一个名为Win32MessageBox包装,在User32.dll中每个.net应用程序调用该对象的Show方法调用时MessageBox函数。
它requeres System.Runtime.InteropServices命名空间。

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

要调用它,只需键入:

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

在这里你拨打上面一行的方法并不需要知道,这是一个实际调用在非托管的DLL函数。

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.

资源:由托尼诺思拉普的MCTS自学培训工具包(考试70-536)

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

这篇关于如何消耗ASP.NET类文件COM对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆