سؤال

لدي كائن COM الذي أحاول التفاف في فئة C # من أجل جعلها متاحة بسهولة للتطبيقات الأخرى التي ترغب في استهلاكها.

لدي التعليمات البرمجية التالية التي تنشئ مثيل كائن COM، ثم استخدام الانعكاس إجراء مكالمة إلى طريقة لاسترداد بيانات المستخدم. يعمل هذا الرمز جيدا عند وجوده في صفحة ASPX. giveacodicetagpre.

ومع ذلك، عندما أقوم بنقل الرمز إلى ملف فئة (jd_api.cs) من أجل ملخصها من الموقع الفعلي، لم أعد أستطيع الحصول عليها للعمل. على سبيل المثال، لدي الطريقة الثابتة التالية التي تم الإعلان عنها مثل هذه: giveacodicetagpre.

لسوء الحظ، يتم تقييد كائن الخادم على بعض مكتبات ASP.NET المضمنة بشكل افتراضي في تطبيقات الويب، وبالتالي كان الرمز المذكور أعلاه لا يذهب. لذلك في هذه المرحلة قررت محاولة إنشاء مثيل لكائن COM مثل هذه: giveacodicetagpre.

ومع ذلك في وقت التشغيل، أحصل على خطأ يقول " حاول قراءة أو كتابة ذاكرة محمية. غالبا ما يكون هذا مؤشرا على أن الذاكرة الأخرى تفسد. ".

لست متأكدا من أين أذهب من هنا. أي مساعدة حول كيفية التجردة لإنشاء مثيل لكائن COM هذا إلى طبقة غير ضمن تطبيق الويب، ستقدر بشكل كبير. شكرا!

هل كانت مفيدة؟

المحلول

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