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++ / November 2005

Tip: Looking for answers? Try searching our database.

using pinvoke to fill a string

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
brian_harris - 23 Nov 2005 20:56 GMT
I have a 3rd party unmanged .dll. This has functions that will look up
information and fill a C string passed to it with data.  I want to use this
data in .NET classes after it has been filled and I am programming in C++.  I
believe that I should use pinvoke, but I see a problem that I can't figure
how to get around.  I don't think I can declare the string I want as a String
class since when I pass it to unmanged function it is not yet allocated and
the unmanged function does not allocate space it just fills it.  So I would
think this would cause memory corruption.  I was also thinking that I could
create the String class and fill it with blanks so memory was allocated, but
I don't know if the marshling will correctly change value since String class
are basically a static value unless you use correct member function that
deletes old value and then adds new value.

If any one can tell me the correct way to set this up it would be very much
appreciated.
Thanks
JAL - 23 Nov 2005 22:37 GMT
Brian.. In C++/cli given"

        bool Copy(char* out,int size,const char * in) {
            strcpy_s(out,size,in);
            bool isNullTerminated= false;
            if (in[size-1] == '\0')
                isNullTerminated= true;
            return isNullTerminated;
        }

You can do:

// exercise String to Ansi Ansi to String
String^ Echo(String^ in) {
    NativeChild nc;
    IntPtr ip(0);
    String^ out=L"";
    char* str= 0;
    try {
        str= new char[in->Length+1]; // null terminator
        ip = Marshal::StringToHGlobalAnsi(in); // create char[] on unmanaged heap
        nc.Copy(str,in->Length+1,static_cast<const char*>(ip.ToPointer()));
        out= Marshal::PtrToStringAnsi(static_cast<IntPtr>(str));
    }
    catch(...) {
        out= L"exception thrown";
    }
    finally {
        if (ip != IntPtr(0)) {
            Marshal::FreeHGlobal(ip);  // release char[] on unmanaged heap
        }
        if(str) {
            delete [] str;  // release char[] on unmanaged heap
        }
    }
    return out;
}

:)

> I have a 3rd party unmanged .dll. This has functions that will look up
> information and fill a C string passed to it with data.  I want to use this
[quoted text clipped - 12 lines]
> appreciated.
> Thanks
brian_harris - 29 Nov 2005 16:31 GMT
Thanks, that got me a bit further.  If you don't mind I do have a couple of
questions about your syntax.
You use the declaration of a string as String^ not String * which is what I
had to use to get it to work.
Also in your intilization of out you use L"" , I have seen programs using
S"some string", but not L.  I have not been able to find any documentation on
what these stand for or when you need to use them.  If you could tell me
where to find information on this it would be appreciated.  I presume that
there are also other prefixes that have some other meanings.

> Brian.. In C++/cli given"
>
[quoted text clipped - 52 lines]
> > appreciated.
> > Thanks

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.