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++ / June 2007

Tip: Looking for answers? Try searching our database.

Wrapper Component in C++/CLI to use Legacy C++ code/functionality in C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Subodh - 25 Jun 2007 16:12 GMT
Hi,

We have legacy code in C/C++, I am writing a wrapper component in C++/
CLI that will allow using this legacy code functionality in C#, I have
linked all my static libraries to this C++/CLI DLL project
I have created a wrapper public static ref  class in C++/CLI project
that will expose functions for the C# client.

Problem:
when I use a function that returns a pointer or that has pointer
arguments, in the C++/CLI project, The C# client where this wrapper
component is referenced, gives a runtime error when i try to access
the wrapper function,
Error:
"The handle is invalid. (Exception from HRESULT: 0x80070006
(E_HANDLE))"

How do I use functions that accept/return pointer arguments in my C++/
CLI component so that I can use the functionality in C#??

C++/CLI code
#pragma managed
public ref class LibFunctions
{
public:
    static String^ GetDefaultMgr(void)
    {
          get_default_manager(); //this is a c function that returns
a TCHAR*
      }
};

C# client code:
  private void DefMgr_Click(object sender, EventArgs e)
       {
           try
           {
               MessageBox.Show(LibFunctions.GetDefaultMgr());
           }
           catch (Exception exception)
           {
               MessageBox.Show(exception.Message);
           }
       }
Ben Voigt [C++ MVP] - 25 Jun 2007 18:12 GMT
> Hi,
>
[quoted text clipped - 25 lines]
>           get_default_manager(); //this is a c function that returns
> a TCHAR*

Here you have a non-void function without a return statement.  You should
have gotten a compiler warning for that, and naturally the result is no
good.

Make sure you return a valid String^, created using Marshal::PtrToStringAnsi
or Marshal::PtrToStringUni depending on whether UNICODE is defined.

>       }
> };
[quoted text clipped - 11 lines]
>            }
>        }

Rate this thread:







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.