Click to See Complete Forum and Search --> : Error While Passing String to Com Component


arvindnama
April 8th, 2008, 07:59 AM
hi,

I ve built a COM component with 2 interfaces (IString and IMath). and i've a C# client whic use the functionalities in IString ...

Since
COM was built in C++ (Unmanaged) n
C# is managed
i built a wrapper using C++/CLI to create a COM Component get Interface ptrs n marshalling etc...

there is 1 function in IString one to concatinate 2 string ... so i/n is 2
char * 's...

i create a string (say s1) in C# pass it to C++/CLI ...
here i marshall String s1 into char * (say string 1)...
then pass string1 as argument to concat function...

marshaling is done successfully but when i pass the string1 to com fuction concat... string1 changes to something else ......
this change follows a patterns
i.e
if string1="Arvind" n string2="Nama"...
after passing to com component

string1 become "AN" and string2 become "N"..
i.e 1st argument becomes 1st char of string1("A") n 2nd char of string2("N")
and 2nd argument is 1st char of string2("N")...
dont know what to do ...
i though i was doing something wrong in Marshaling
but if print the the unmarshalled n marshalled string ...they produce proper results...

btw i use StringToGlobalAnsi() -to convert managed string to unmanaged char*

code snippet:::


String^ W_CA::W_Concat(String^ s1,String^ s2)
{
CHAR *string1,*string2;

//Marshaling managed s1 to unmanaged string1
string1=convertManagedToUnmanaged(s1);

//Marshaling managed s2 to unmanaged string2
string2=convertManagedToUnmanaged(s2);


/Allocationg space on unmanaged heap to store result
char *res=new char[s1->Length+s2->Length];

//actuall call to COM Component
p_Is->Concat(string1,string2,res);

String ^Result;

//Marshaling unmanaged string res to Result

Result=convertUnmanagedToManaged(res);

//Releasing unmanaged memory
release(string1);
release(string2);

return Result;

}


char* W_CA::convertManagedToUnmanaged(String^ s)
{
char * string=new char[s->Length];
IntPtr us=Marshal::StringToHGlobalAnsi(s);
string=static_cast<char *>(us.ToPointer());
return string;
}

String^ W_CA::convertUnmanagedToManaged(char* s)
{
IntPtr sp=static_cast<IntPtr >(s);
String^ Res=Marshal::PtrToStringAnsi(sp);
return Res;
}

void W_CA::release(char* s)
{
IntPtr sp=static_cast<IntPtr >(s);
Marshal::FreeHGlobal(sp);

}


PLZZZZZZZZZZZZZZZ.......... HELP ME .... URGENT

Alex F
April 9th, 2008, 04:52 AM
Try this:

String^ W_CA::W_Concat(String^ s1,String^ s2)
{
CHAR *string1,*string2;

//Marshaling managed s1 to unmanaged string1
string1=convertManagedToUnmanaged(s1);

//Marshaling managed s2 to unmanaged string2
string2=convertManagedToUnmanaged(s2);


/Allocationg space on unmanaged heap to store result
char *res=new char[s1->Length+s2->Length + 1];

//actuall call to COM Component
p_Is->Concat(string1,string2,res);

String ^Result;

//Marshaling unmanaged string res to Result

Result=convertUnmanagedToManaged(res);

//Releasing unmanaged memory
release(string1);
release(string2);

return Result;

}


char* W_CA::convertManagedToUnmanaged(String^ s)
{
return (char*)Marshal::StringToHGlobalAnsi(s).ToPointer();
}

String^ W_CA::convertUnmanagedToManaged(char* s)
{
return Marshal::PtrToStringAnsi(IntPtr((void*)s));
}

void W_CA::release(char* s)
{
IntPtr sp((void*)s);
Marshal::FreeHGlobal(sp);

}

arvindnama
April 9th, 2008, 10:10 AM
Thanx alot for the help....


actually the mistake was i was marshaling String ^ to char * ....which wont give correct results in COM....

so instead i used BSTR..... it workes properly now ...

i.e instead of using Marshl::StringtoHGlobalAnsi(...) use StringtoBSTR(..)...