NullReferenceException in MC++

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I am new to C++ & MC++. I am trying to write a wrapper class for exsiting
code which uses libraries(Adobe Framemaker) to open documents. These
libraries are written in C++.

The code is :-

.......
public __gc class Framemaker
{

bool ConnectToFrameSession(String* strProgID)
{
char* strProg = (char*)(void*)Marshal::StringToHGlobalAnsi(strProgID);
ConnectToFrame(strProg);

return true;
}
.......

int ConnectToFrame(char *strProg)
{
StringT opt_progid;
CLSID pclsid;
LPOLESTR progStr;
HRESULT res;
F_ObjHandleT docId;

// Get the process name.
opt_progid = F_StrCopyString((StringT)strProg);

// Convert the process name into a GUID
progStr = (OLECHAR*)malloc( WBUFLEN*sizeof(wchar_t) );
if(0 == MultiByteToWideChar(CP_ACP, 0, (char *)opt_progid, -1, progStr,
WBUFLEN ))
{
fprintf(stderr, "failed to allocate\n");
return(1);
}
........

Now the code bombs while doing a malloc or calling any native frame
functions in the function above. It says Object Not Set or NullRefernecce
Exception.

Is it beacuse i am trying to mix managed and unmanaged code?
How shld i go abt solving ths problem ?

Also this code runs fine if i make a console application & run it.
I am trying to make wrapper dll which would be calkled by C#.
Only differnec i see is __GC.

Pls guide me.

Thanks,
Gurminder
 
Gurminder said:
Hi,

I am new to C++ & MC++. I am trying to write a wrapper class for exsiting
code which uses libraries(Adobe Framemaker) to open documents. These
libraries are written in C++.

The code is :-

......
public __gc class Framemaker
{

bool ConnectToFrameSession(String* strProgID)
{
char* strProg = (char*)(void*)Marshal::StringToHGlobalAnsi(strProgID);
ConnectToFrame(strProg);

return true;
}

I don't think this is the problem, but you're missing a call to
Marshal::FreeHGlobal(strProg), which is a memory leak. Also, I'd use
..ToPointer() instead of casting to void*. That void* casting looks very
suspicious to me.

Can you try this?

bool ConnectToFrameSession(String* strProgID)
{
IntPtr hProg = Marshal::StringToHGlobalAnsi(strProgID);
char* strProg = reinterpret_cast<char*>(hProg.ToPointer());
ConnectToFrame(strProg);
Marshal::FreeHGlobal(hProg);
return true;
}

I'm using the above code without any problem in my managed->unmanaged
interface.

Or possibility your unmanaged function does something wrong, which I
can't verify.

Tom
 
Back
Top