2010-07-23 2 views
3

일부 C++ 코드를 연결하고 C#을 통해 액세스 할 수있는 COM 개체로 래핑하려고했습니다. 나는 atl 프로젝트를 만들고 Add (double a, double b)와 같은 간단한 메소드를 추가했다.C#의 ATL dll 파일에서 액세스하는 메서드

// atl.h : Declaration of the Catl 
#pragma once 
#include "resource.h"  // main symbols 

#include "com_i.h" 


#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA) 
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms." 
#endif 



// Catl 

class ATL_NO_VTABLE Catl : 
public CComObjectRootEx<CComSingleThreadModel>, 
public CComCoClass<Catl, &CLSID_atl>, 
public Iatl 
{ 
public: 
Catl() 
{ 
} 

DECLARE_REGISTRY_RESOURCEID(IDR_ATL) 

DECLARE_NOT_AGGREGATABLE(Catl) 

BEGIN_COM_MAP(Catl) 
COM_INTERFACE_ENTRY(Iatl) 
END_COM_MAP() 



DECLARE_PROTECT_FINAL_CONSTRUCT() 

HRESULT FinalConstruct() 
{ 
    return S_OK; 
} 

void FinalRelease() 
{ 
} 

public: 

STDMETHOD(Add)(DOUBLE a, DOUBLE b); 
}; 

OBJECT_ENTRY_AUTO(__uuidof(atl), Catl) 

다음 난 후에 나는 DLL을 호출하는거야 내 C# 파일 ... 안쪽 atl.cpp 파일

// atl.cpp : Implementation of Catl 

#include "stdafx.h" 
#include "atl.h" 

STDMETHODIMP Catl::Add(DOUBLE a, DOUBLE b) 
{ 
// TODO: Add your implementation code here 

return a + b; 
} 

에서이다 : 다음은 내 atl.h 파일의 코드 그것을 참조 ... dll하지만 할당 된 메서드를 볼 수 없습니다. 내 문제 야. program.cs의 코드를 인용하십시오.

sing System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Runtime.InteropServices; 

namespace sharpdll 
{ 
class Program 
{ 
    [DllImport("com.dll")] 
    public static extern double Add(double a, double b); 

    static void Main(string[] args) 
    { 
     Add(2, 3); 
    } 
} 
} 

Add (2, 3); " 'com.dll'DLL에 '추가'라는 엔트리 포인트를 찾을 수 없습니다." 아이디어가 있으십니까?

답변

2

DllImport는 PInvoke (기본 Win32 dll에 해당) 용입니다.
COM Interop이 필요합니다.

.Net 또는 COM 구성 요소와 마찬가지로 ATL com 개체를 등록한 다음 해당 개체에 대한 참조를 추가하십시오.

ATL 대신 C++/CLI를 통해 C++ 기능을 노출 할 수 있습니다.

호프가 도움이 되었기를 바랍니다.

+0

올바른 방향으로 안내 해주신 고맙습니다. 그러나 개체를 등록한 후 프로젝트의 bin 폴더에서 dll을 평소와 같이 참조하여 프로젝트를 실행할 때 다음과 같은 오류가 발생합니다. DLL 'com.dll'을로드 할 수 없습니다. 지정된 모듈을 찾을 수 없습니다. (Exception from HRESULT : 0x8007007E) 왜이 오류가 발생했는지 ... 참조가 제대로 작동했는지 이해할 수 없습니까? 거의 다 왔어!! – user400383

+0

스카이프, 크로스 루프 등을 통해 모든 것을 확인하기 위해 내 화면을 보여주고 싶습니다. – user400383

+0

COM 개체를 올바로 작성하지 않는다고 생각합니다. 제 전문 분야가 아닙니다. 이 특정 문제에 대해 다른 질문을해야합니다. 미안 나는 더 많은 도움이 될 수 없다 :) –

관련 문제