Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2005

Tip: Looking for answers? Try searching our database.

NullReferenceException in MC++

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gurminder - 10 May 2005 22:46 GMT
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
Tamas Demjen - 10 May 2005 23:28 GMT
> Hi,
>
[quoted text clipped - 15 lines]
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.