"TestDllC.h" public class TestDllC
{
private:
int bb;
static int pid;
static TestDllC *pThis;
private:
TestDllC();
~TestDllC();
!TestDllC();
static TestDllC(); public:
static TestDllC* GetInstance();
int MessageSay();
};"TestDllC.cpp"
TestDllC::TestDllC()
{
pid++;
} TestDllC::~TestDllC()
{

}
TestDllC::!TestDllC()
{
// delete pThis; 
}
static TestDllC::TestDllC()
{
pid=0;
} static TestDllC* TestDllC::GetInstance()
{
if(pThis==NULL)
{
pThis=new TestDllC();//一个创建不复杂的类 所以不考虑线程安全
}
return pThis;
// return gcnew TestDllC();
} int TestDllC::MessageSay()
{
return pid;
}类的示例如上,如何来做?

解决方案 »

  1.   

    我之前这么做的,你看看有没有帮助
    DLL第一个方法,无参数,创建C++中类的实例,返回实例指针
    C#中存储此指针
    DLL中后面的方法,第一个参数为刚才的类指针,然后是其他参数
    C#调用的时候,根据指针找到这个类的实例,再调用实例的方法
      

  2.   


    第一个方法是哪个?上面那几个方法嘛。我的目的是要把示例的代码做成一个Dll,供C#的应用程序调用。
    无论怎么创建 怎么修改设置 都会提示pThis=new TestDllC();这里不能使用new 还有static TestDllC *pThis这里提示不能使用不确定的类型,也就是指针的问题。
      

  3.   

    这 C++ 代码是已经编译好的 Dll 的还是只是代码准备编译? 如果是准备编译就把 class 里的静态字段和方法提出来,按 C 语言方式,结构加函数,不然 C# 调用时符号连接都很麻烦
      

  4.   

    不行。在C#中不能调用。
    1.把动态库改造成Com接口或者全部以方法引出,不要使用类。
    2.自己使用C++再次封装,以方法引出。
      

  5.   

    正在研究Com中,不过好像也不能使用Static修饰。C++的Dll:
    // TestDll.h#pragma onceusing namespace System;namespace TestDll 
    {
    public ref class TestDllC
    {
    private:
    int iPid;
    static int iBasePid;
    public:
    TestDllC();
    ~TestDllC();
    !TestDllC();
    static TestDllC(); public:
    int TestFunc();
    };
    }C++Dll的TestDll.cpp// 这是主 DLL 文件。#include "stdafx.h"#include "TestDll.h"
    namespace TestDll 
    {
    TestDllC::TestDllC()
    {
    iPid = iBasePid++;
    } TestDllC::~TestDllC()
    { }
    TestDllC::!TestDllC()
    {
    // delete pThis; 
    }
    static TestDllC::TestDllC()
    {
    iBasePid = 0;
    } int TestDllC::TestFunc()
    {
    return iPid;
    }
    }程序集说明里添加了关于强命名的几个小属性,也就是随便SN -k了一个文件 然后添加进去
    C# Com添加了上述Dll的引用代码如下:using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;using TestDll;namespace MyCom
    {
        [ComVisible(true)]
        [Guid("96C2B813-A76C-41a5-93D6-A4B995F70AA0")]
        public interface IComTest1
        {
            static IComTest1 GetInstance();
            void Initialize();
            void Dispose();
            int Test();    }    [Guid("ACA89BB6-2E04-42d7-840D-ECAEA5CCF5C1"), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
        public interface MyCom_Events
        {
        }
        [ProgId("MyCom.MyClass")]
        public class ComTest1:IComTest1
        {
            private static TestDllC iTestDllC;
            private static IComTest1 iTestCom;        private ComTest1()
            {        }
            static ComTest1()
            {
                iTestCom = null;
                iTestDllC = new TestDllC();
            }
            public void Initialize()
            {        }
            public void Dispose()
            {        }
            public int Test()
            {
                return iTestDllC.TestFunc();
            }
            static public IComTest1 GetInstance()
            {
                if (iTestCom == null)
                {
                    lock (iTestCom)
                    {
                        if (iTestCom == null)
                        {
                            iTestCom = new ComTest1();
                        }
                    }
                }
                return iTestCom;
            }
        }
    }编译提示错误 1 修饰符“static”对该项无效 求如何在对Com调用GetInstance方法实现返回一个引用或者实例指针之类的?
      

  6.   

    总之 我的意图是不愿意在应用程序中可以创建多个类的实例,虽然GetInstance方法不设置static可以实现创建多个ComTest实例都对一个TestDll实例操作,但是不完美,想只能创建一个ComTest的实例