By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,906 Members | 1,700 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,906 IT Pros & Developers. It's quick & easy.

calling C++ method from C# code - 2nd attempt

P: n/a
I'm having a really hard time with wrapping an unmanaged class with a
managed class, then calling that managed class from my C# code.

I will show you the three pieces, then explain:
-------- Unmanaged C++ code ---------
bool FindFileItem(PCSTR filename, IVSSItem& founditem); // note that
founditem is a REFERENCE
-------- Managed C++ code ---------
bool CSUSSWrapper::FindFileItem(String* filename, IVSSItem& founditem)
{
char* pFilename = (char*)(void*)Marshal::StringToHGlobalAnsi(filenam e);
bool result = m_objSourceSafe->FindFileItem(pFilename, founditem);
// m_objSourceSafe is the unmanaged class
Marshal::FreeHGlobal(pFilename);
return result;
}
-------- C# code ---------
IVSSItem item;
sourceSafe.FindFileItem(shortYear, &item) // sourceSafe is
themanaged wrapper object


so, far starters you may have noticed that the C# code appears to be sending
a IVSSItem* instead of IVSSItem&, this I can't help. I get compiler errors
if I pass just IVSSItem, intellisense tells me that it wants a ptr, sooo...?
BTW, IVSSItem is a class derived from COleDispatchDriver
The problem I'm having is I get this error (somewhat randomly):

An unhandled exception of type 'System.NullReferenceException' occurred in
suabwrapper.dll
I suspect that the problem is happening in the wrapper C++ class. I'm
passing in a pointer to an IVSSItem from the C# code, but the C++ code is
treating it as a reference(I think) - to be honest, I really don't know what
is happening. I get random crashes and I have been able to snuff them out
by modifying the body of the UNMANAGED method FindFileItem() to ignore the
passed in IVSSItem reference and instead create a local IVSSItem (rendering
the function useless, but stops crashes)
I would REALLY appreciate any help you can offer, I don't know what else to
try.
Nov 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.