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 / .NET Framework / Interop / March 2007

Tip: Looking for answers? Try searching our database.

C# to C++ Dll with a pointer to string.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
asnowfall@gmail.com - 28 Mar 2007 22:16 GMT
I have a C++ DLL that accepts BSTR* and changes its value before
returning. I am calling this API from C#.
I get changed value in Object[] but not in actual variable. Please see
my code snippet to understand.

C++ DLL API:-
void Fun(BSTR* data) ;

C# wrapper:-
[DllImport("Test.dll", EntryPoint = "Fun", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
       public static extern void Fun(ref string data);

Calling from C#:-
string sHash = "";
Object[] objMethodParams ={ sHash };
MethodInfo.Invoke("", objMethodParams);

After the call, objMethodParams[0] shows the new changed string but
'sHash'  does not get updated with new value and still shows "".

I tried changing C# wrapper from 'void Fun(ref string data)' to 'void
Fun([in out] string data)' and this change made things worse, even
objMethodParams[0] started showing "".

Please let me know if you know solution.

Thanks
Ramesh
Mattias Sjögren - 29 Mar 2007 06:21 GMT
>Please let me know if you know solution.

sHash = (string)objMethodParams[0];

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

asnowfall@gmail.com - 29 Mar 2007 16:15 GMT
On Mar 29, 1:21 am, Mattias Sjögren <mattias.dont.want.s...@mvps.org>
wrote:
> >Please let me know if you know solution.
>
> sHash = (string)objMethodParams[0];

I thought that is a work around and I have been using it. Since
objMethodParams[0] contains reference to 'sHash'; Is it not 'sHash'
supposed to have the returned value?
My motive to post was to know why this is behaving this way.

Thanks
Ramesh
Mattias Sjögren - 30 Mar 2007 05:51 GMT
>Is it not 'sHash' supposed to have the returned value?

No. Strings in .NET are immutable so they can't change. Instead you
get a new string object back.

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

asnowfall@gmail.com - 30 Mar 2007 19:21 GMT
On Mar 30, 12:51 am, Mattias Sjögren <mattias.dont.want.s...@mvps.org>
wrote:
> >Is it not 'sHash' supposed to have the returned value?
>
> No. Strings in .NET are immutable so they can't change. Instead you
> get a newstringobject back.
>
> Mattias

Thanks
Ramesh

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.