C++ 클래스 인스턴스를 C# 프로그램이 넘겨 받을 때 프로퍼티...

hiluciano의 이미지

아래 내용은 MyDLL.cpp 입니다.

class MYCLASS
{
public:
MYCLASS()
{
this->_foo = -1;

const wchar_t* temp = L"MYCLASS Instance";
int tempsize = (wcslen(temp) + 1) * sizeof(wchar_t);

this->_bar = (wchar_t *)malloc(tempsize);
memset(this->_bar, 0, tempsize);
memmove(this->_bar, temp, tempsize);
}
int fooGet() { return this->_foo; }
void fooSet(int value) { this->_foo = value; }

wchar_t* barGet() { return this->_bar; }
void barSet(wchar_t* value) { this->_bar = value; }

private:
int _foo;
wchar_t* _bar;
};

extern "C" __declspec(dllexport) MYCLASS * CreateMyClassInstance()
{
MYCLASS * retval = new MYCLASS[1];
*retval = MYCLASS();
return retval;
}

---------- 아래는 C# 소스입니다. ----------
public interface MYCLASS
{
int foo { get; set; }
string bar { get; set; }
}

public static class Program
{
[DllImport("MyDLL.dll", EntryPoint = "CreateMyClassInstance")]
public extern static MYCLASS CreateInstance();

public static void Main()
{
MYCLASS instance = CreateInstance();
Console.WriteLine("foo == {0}", instance.foo);
instance.foo = 16;
Console.WriteLine("foo == {0}", instance.foo);

Console.WriteLine("bar == {0}", instance.bar);
instance.bar = "C# Instance";
Console.WriteLine("bar == {0}", instance.bar);
}
}

원래대로라면 C++ 클래스에서 fooGet/Set barGet/Set 으로 멤버함수를 선언했기 때문에
C# 인터페이스에서도 fooGet/Set, barGet/Set으로 선언해야 합니다.

그런데 C#에선 프로퍼티가 지원 되니까 C++ 클래스처럼 그대로 Get/Set 이 들어간 멤버함수를
쓰느니 차라리 두 함수를 C#에서는 프로퍼티로 묶어서 처리 하고 싶습니다.

그렇게 하려면 C# 프로퍼티에서 get, set 작동이 들어올 때 내부적으로 fooGet, fooSet 함수가
호출되도록 무언가 특성을 지정해 주어야 할듯 싶은데

DllImport 특성은 EntryPoint로 외부 함수의 진입점을 명시적으로 지정할 수 있듯이
프로퍼티에서도 마찬가지로

instance.foo = 16; 과 같은 코드가 있을 때 이것을 내부적으로
원래 C++ 클래스에서 선언한 멤버 함수인 fooSet 함수가 호출되도록 할 수 있을까요?

(C++에서 프로퍼티를 구현하는 것이 아닙니다.)

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.