Click here to Skip to main content
15,917,328 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralRe: No output from << operator in console app - code correction Pin
malmi18-Jan-03 6:02
malmi18-Jan-03 6:02 
GeneralStupid question about VC6 to VC.Net migration reason Pin
Member 13901616-Jan-03 0:23
Member 13901616-Jan-03 0:23 
GeneralSystem::String/StringBuilder as out parameter Pin
Paul Selormey14-Jan-03 22:23
Paul Selormey14-Jan-03 22:23 
GeneralRe: System::String/StringBuilder as out parameter Pin
Jeff J15-Jan-03 10:16
Jeff J15-Jan-03 10:16 
GeneralRe: System::String/StringBuilder as out parameter Pin
Paul Selormey15-Jan-03 15:40
Paul Selormey15-Jan-03 15:40 
GeneralRe: System::String/StringBuilder as out parameter Pin
Paul Selormey15-Jan-03 15:48
Paul Selormey15-Jan-03 15:48 
GeneralRe: System::String/StringBuilder as out parameter Pin
Jeff J16-Jan-03 13:29
Jeff J16-Jan-03 13:29 
GeneralHelp me!!! A issue of wrapping unmanaged class with Managed C++ Pin
Anonymous14-Jan-03 3:23
Anonymous14-Jan-03 3:23 
I met a problem when I tried to wrap unmanaged class into managed class with managed C++.

First, I built a static library, VcClass.lib, from the following source code with VC++ 6.0 :

/* interface.h */
#include <windows.h>

class CMyClassA
{
public:
BYTE *m_pBuf;
int m_bufLen;

CMyClassA() { m_pBuf = NULL; m_bufLen = 0; }
~CMyClassA() { FreeBuf(); }

HRESULT LoadBuf(BYTE *pBuf, DWORD dwBufLen);

private:

HRESULT FreeBuf(void);
};

class CMyClassB
{
public:
CMyClassB() { m_pA = NULL; }
~CMyClassB() { m_pA = NULL; }

HRESULT Func(CMyClassA *pClassA);
HRESULT Func2(void);

private:
CMyClassA *m_pA;
};

#endif

/* VcClass.cpp */
#include <stdio.h>
#include "interface.h"

HRESULT CMyClassA::LoadBuf(BYTE *pBuf, DWORD dwBufLen)
{
m_bufLen = dwBufLen;
m_pBuf = new BYTE[m_bufLen];
memcpy(m_pBuf, pBuf, m_bufLen);

return 0;
}

HRESULT CMyClassA::FreeBuf(void)
{
m_bufLen = 0;
delete [] m_pBuf;

return 0;
}

HRESULT CMyClassB::Func(CMyClassA *pClassA)
{
m_pA->m_pBuf = pClassA->m_pBuf;
m_pA->m_bufLen = pClassA->m_bufLen;

for (int i=0; i<m_pa->m_bufLen; i++)
printf("buf[%d] = %d\n", i, m_pA->m_pBuf[i]);

return 0;
}

HRESULT CMyClassB::Func2(void)
{
if (m_pA->m_pBuf == NULL) {
printf("buf is empty, call Func() first!\n");
return 1;
}

for (int i=0; i<m_pa->m_bufLen; i++)
printf("buf[%d] = %d\n", i, m_pA->m_pBuf[i]);

return 0;
}

then, I wrote two wrapped classes with VC++ 7.0 and compiled them into a DLL:

#include "interface.h"

using namespace System;

namespace McClass
{
public __gc class McMyClassA
{
public:
McMyClassA() { m_pClassA = new CMyClassA; }
~McMyClassA() { delete m_pClassA; }

int LoadBuf(System::Byte Buf[], int Len)
{
System::Byte __pin * p = &Buf[0];

return m_pClassA->LoadBuf(p, Len);
}

void *GetA() { return (void*)m_pClassA; }

private:
CMyClassA * m_pClassA;
};

public __gc class McMyClassB
{
public:
McMyClassB() { m_pClassB = new CMyClassB; }
~McMyClassB() { delete m_pClassB; }

int Func(McMyClassA *pMA)
{
CMyClassA *pA = (CMyClassA*)pMA->GetA();

return m_pClassB->Func(pA);
}

int Func2(void)
{
return m_pClassB->Func2();
}

private:
CMyClassB * m_pClassB;

};
}


Finally, I wrote an application with C# to call the DLL:

using System;
using McClass;

namespace CSharpApp
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int i;

McMyClassA classA = new McMyClassA();
McMyClassB classB = new McMyClassB();

Byte[] buf = new Byte[10];
for (i = 0; i<10; i++)
buf[i] = 54;

classA.LoadBuf(buf, 10);
classB.Func(classA);
Console.WriteLine("OK.");

classB.Func2();
Console.WriteLine("OK.");
}
}
}


When I run the C# application, a NullRefrenceException occurred and the additional information is :

Unhandled Exception: System.NullReferenceException: Object reference not set to
an instance of an object.
at CMyClassB.Func(CMyClassB* , CMyClassA* )
at McClass.McMyClassB.Func(McMyClassA pMA)
at CSharpApp.Class1.Main(String[] args)Cry | :((

I think that the exception occurs because I used a wrong approach to pass a pointer of MyClassA into an instance of MyClassB.
So, what is the right way to do it? Data Marshal?
I hope MyClassA is independent on MyClassB, therefore I don’t want to merge MyClassA and MyClassB into one class.
Please help me!
Thank you very much!
GeneralRe: Help me!!! A issue of wrapping unmanaged class with Managed C++ Pin
Anonymous14-Jan-03 21:43
Anonymous14-Jan-03 21:43 
GeneralRe: Help me!!! A issue of wrapping unmanaged class with Managed C++ Pin
Paul Selormey14-Jan-03 22:25
Paul Selormey14-Jan-03 22:25 
Generalstatic constructors - still cannot get it! Pin
Paul Selormey14-Jan-03 0:55
Paul Selormey14-Jan-03 0:55 
Generalstd::string marshalling Pin
Member 1043939-Jan-03 12:39
Member 1043939-Jan-03 12:39 
GeneralRe: std::string marshalling Pin
Jeff J9-Jan-03 20:24
Jeff J9-Jan-03 20:24 
Generalweb project with Visual C++ Pin
naradaji8-Jan-03 4:23
naradaji8-Jan-03 4:23 
GeneralRe: web project with Visual C++ Pin
AlexO8-Jan-03 5:23
AlexO8-Jan-03 5:23 
GeneralRe: web project with Visual C++ Pin
naradaji8-Jan-03 20:54
naradaji8-Jan-03 20:54 
QuestionHow can build an exe file by VS .NET to execute it on Windows_98? Pin
Behzad Ebrahimi7-Jan-03 0:13
Behzad Ebrahimi7-Jan-03 0:13 
AnswerRe: How can build an exe file by VS .NET to execute it on Windows_98? Pin
Paul Ingles7-Jan-03 1:26
Paul Ingles7-Jan-03 1:26 
AnswerRe: How can build an exe file by VS .NET to execute it on Windows_98? Pin
Brian Olej8-Jan-03 13:00
Brian Olej8-Jan-03 13:00 
Generalcaught between managed and unmanaged C++ Pin
Member 10439331-Dec-02 5:26
Member 10439331-Dec-02 5:26 
GeneralRe: caught between managed and unmanaged C++ Pin
Jeff J1-Jan-03 18:22
Jeff J1-Jan-03 18:22 
Generallinking error Pin
EyesOfTruth24-Dec-02 6:57
EyesOfTruth24-Dec-02 6:57 
GeneralDirectX 9.0 Released Pin
Heath Stewart20-Dec-02 9:29
protectorHeath Stewart20-Dec-02 9:29 
GeneralUsing DLL's in Managed C++ Pin
zaza_nata19-Dec-02 20:02
susszaza_nata19-Dec-02 20:02 
GeneralRe: Using DLL's in Managed C++ Pin
Anders Molin20-Dec-02 12:02
professionalAnders Molin20-Dec-02 12:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.