|
This is the best solution I ultimately came up with. It's not all that satisfying, because I'd think there'd be something that allowed me to do the equivalent of strcpy from a Byte[] or a char[], but if there is, then I"m unaware of it.
// Populates the specified buffer with the
// UTF-8 encoded, null-terminated C-string
// logically equivalent to the specified converts
// from a Unicode .NET string
void Convert(String *unicodeText, char buffer[], int buflen)
{
Convert(unicodeText, char buffer[], int buflen, Encoding::UTF8);
} // wrapper to a more generic function
void Convert(String *unicodeText, char buffer[], int buflen,
Encoding *encoding)
{
Byte bytes[] = encoding->GetBytes(unicodeText);
if (bytes->Count> buflen - 1) throw new Exception();
for (int i = 0; i < bytes->Count; i++)
buffer[i] = bytes[i]; // char is assignable to System::Byte
buffer[bytes->Count] = '\0';
}
A UTF-8 representation of "hello" is just like the traditional ASCII representation, since all five characters are faithfully mapped to numbers less than 128: UTF-8 and ASCII are the same. But German strings containing
umlauts and eszets, such as "FluBen" (where we'll pretend that the u is a really an umlauted u and the B is the double ess aka eszet) require more bytes. "FluBen" would marhal to 8 bytes, because the center two characters have UTF8 mappings that are two bytes wide.
If anyone knows of a more official solution to this, then I'd be delighted to hear of it. Thanks for your time.
Jerry
|
|
|
|
|
Hi. I just have a little question: Is it possible to see an DLL import's map? When I tried to do so in a managed c++ assembly, instead of "XXX.dll" i saw a commented "No map", but in 'normal' (c# or vb.net assemblies), i see all the things. There should be a way to see the map(the name of the dll holding the exported function in an managed c++ exe), right?
Here's what I got when I tried to peek at an dll import using ILDASM:
.method public static pinvokeimpl(/* No map */)
int32 modopt([Microsoft.VisualC]Microsoft.VisualC.IsLongModifier) modopt([mscorlib]System.Runtime.CompilerServices.CallConvStdcall)
ThemepDemoCheck(uint16 modopt([Microsoft.VisualC]Microsoft.VisualC.IsConstModifier)* A_0) native unmanaged preservesig
{
.custom instance void [mscorlib]System.Security.SuppressUnmanagedCodeSecurityAttribute::.ctor() = ( 01 00 00 00 )
// Embedded native code
// Disassembly of native methods is not supported.
// Managed TargetRVA = 0x001D65D7
} // end of method 'Global Functions'::ThemepDemoCheck
Thanks in advance
|
|
|
|
|
I am currently reading, ".NET and COM: The Complete Interoperability Guide", by Adam Nathan. As a way to work my way through the material (to better understand it), I am creating a Windows Forms project from which I can programmatically manipulate MS Word. The authors recommends using the Type Library Importer (TLBIMP.EXE) to convert the Microsoft Word 10.0 Object Library (MSWORD.OLB) into an Interop Assembly. The assembly is then referenced in the Visual C++ .NET project and placed in the same directory as the compiled executable. This is surprisingly easy, and the assembly can then be inspected with the IL Disassembler (very similar to the way the OLE/COM Object Viewer works). At this point, in your source code you can instantiate COM objects very simply with the new operator. The author describes all this clearly and in great detail in his book.
Anyway, many of the method calls on the COM object involve passing structures to the COM component as a VARIANT. The Interop Marshaler apparently maps the COM VARIANT type to the .NET System::Object type, but, there is no way to pass a struct via a System::Object type to the COM object. So the method call throws an exception if you attempt to pass a System::Object to it (or just doesn't compile in the first place). However, the author has included in his book a code listing which manually marshals the VARIANT type in a given example to a structure which the COM component accepts and the function call then will succeed. It is confusing as hell, frankly, and I don't yet fully understand why it works. The basic idea is to reassemble the assembly after changing the signature of the original method so that it accepts an IntPtr instead of the VARIANT. He then writes a class that is like an Interop chimera, and which converts the original VARIANT into a struct via the IRecordInfo interface (InteropServices) and a couple of functions that you've probably never heard of: GetRecordInfoFromTypeInfo (oleaut32.dll), Marshal::GetITypeInfoForType, and Marshall::GetObjectForIUnknown. (I'm leaving alot of information out of this explanation just for brevity and clarity.)
This seems like an awful lot of trouble to go to, and potentially, you might have to write many of these custom marshalers, all different and complex.
I'm wondering if any of you have had to implement these kinds of marshalers in .NET applications, and if so, just how do they perform, and is the development process as insane as it looks?
Any opinion would be appreciated, thanks.
|
|
|
|
|
Well, apparently, no one has any interest in this subject.
I've spent some time exploring the Word.dll assembly, and as it turns out the methods referred to in the above message were alot more easily implemented than I had originally thought. In the Word assembly (which is a huge library) in most of the methods that I've encountered so far, the Type Library Importer attaches a custom attribute (the MarshalAsAttribute) to the [in] parameters of the methods (which is a .NET object&) that apparently is type cast to an ordinary struct (not a struct contained within a VARIANT), so there was no need to write a custom marshaler for any of the data conversions. And, many of the parameters that are required for function calls on an interface or instantiated object are optional. However, the Visual C++ .NET compiler requires an object be supplied for these optional parameters (which override default settings). Weirdly enough, this is easily accomplished by casting Type::Missing to a System::Object using the dynamic_cast operator (or the __try_cast operator, if you'd rather handle the exceptions) and supplying this as the optional parameter. Example:
Object* OptionalParameter = dynamic_cast<Object*>(Type::Missing);
Where you want to supply a structure for one of the optional parameters, it must be boxed.
There is a help file supplied that describes the various objects that can be instantiated and the interfaces that can be used to make function calls. Unfortunately, the help file is written for Visual Basic programmers, and provides only minimal guidance in writing the C++ code. It does, however, tell you how to obtain the many objects, and essentially what functions you must implement to accomplish various tasks in the Word application.
So, as it turns out, Microsoft Word can be automated fairly easily.
|
|
|
|
|
Ok I have just inherited a piece of C++ .NET code( i feel like a proud PAPA ).
This C++ .NET code in the form of assemblies that interface with another set of C# assemblies which are being called from a ASP.NET project. The problem is that when the ASP .NET project is executed..i can just see the resources on this process going from 72,000K to almost 154,000K in a hurry and progressively getting "worst".
So, my first thought is memory leak in the C++ assemblies. Here are the questions.
1. Can i use #define CRTDBG_MAP_ALLOC to debug memory leaks in dll's.
2. Technically according to MS I should put
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
at the start of my program.
So, since it is a just a class library..where would the _CrtSetDbgFlag go.
thanks
Sameer
|
|
|
|
|
for converting *char to int what is the command? i know it should has easy command but i forgot.
really thank you
|
|
|
|
|
|
i want to read a line with ',' as delimiter. i used this code it showed me that arg[i] is empty inside the while loop. and it was carrying word(speak)before getting into loop. for X here is my first time of using it in program:
char *X;
X = strtok(NULL,",");
while(i < 9)
{
strcpy(arg[i], X);
i++;
X = strtok(NULL, ",");
X = strtok(NULL,",");
}
really thank you
|
|
|
|
|
Hi Guys,
Still fighting with some conversion problems.
Hope someone could help.
//some Typedefs.
typedef unsigned char T_Byte;
typedef std::basic_string<t_byte> T_ByteArray;
I want to convert the following value to a CString.
[1]
T_Byte LineFeed[] = {'\x0D','\x0A'};//Hexadecimal Values.
T_ByteArray valLineFeed;
valLineFeed.append(LineFeed,sizeof(LineFeed));
want to convert the "valLineFeed" to a Cstring so that it contains a string "0D0A".
How could I achieve that?
[2]
Same way,If some one gives me a string "0D0A",would want to convert it to a T_ByteArray which contains characters like '\x0D','\x0A'
In short,If someone gives me a string "0D0A".Want to insert the characters "\x" before "0D" & "0A" & make a T_Byte string
which is like :
T_Byte LineFeed[] = {'\x0D','\x0A'}
How Could I achieve this also..
Thanks in advance..........
|
|
|
|
|
Here is the problem with what you ask. Your questions pertain to fundamental C++ issues. There are books and web based tutorials containing pages that discuss in detail the topic you are asking about. Someone could post a simple code example that depicts what you are asking but that would not help you understand the how and why it works which is what you should be most interested in rather than just the code to do it.
It is unlikely that someone will write a specific message here explaining the topic when it is easily found on the net and in books.
"No matter where you go, there your are." - Buckaroo Banzai
-pete
|
|
|
|
|
How can I convert String to unsigned char __gc[] and vice versa???
Anyone got an idea?
--Nikola--
modified 7-Dec-20 21:01pm.
|
|
|
|
|
Managed String Objects can be initialized directly from unmanaged memory blocks. They will copy the memory for you.
To copy Managed memory into unmanaged memory blocks you need to Marshall the unmanaged memory using the System::Runtime::InteropServices::Marshal class.
Hate is not a family value.
-pete
|
|
|
|
|
This may be the wrong place to ask but where else should I go?
I'm new to .Net and WebServices. However, I'm old in programming MFC/C++ and Java.
I would like to write a WebService (Server) which is NOT running within a IIS (ASP). All I need is a normal managed Windows Service which is listening on a spec. port to publish it's WebService interface. However, there are lots of examples on how to write WebServices using ASP.Net but it seams tobe impossible to accomplish the same with a 'normal' EXE.
Please don't ignore me, let me know your opinion....
Cheers
Christian
|
|
|
|
|
Hi Guys,
Going mad with this....
I have a string object say,
String val("Context");
How Can I convert this "val" to a CString.
Tried as follows,but didn't work
CString test;
test.Format(_T("%s"),val.c_str());
It gave me junk value.....
Any help is appreciated....
|
|
|
|
|
Your first post you asked about "wstring to LPCTSTR"
Now you ask about "String to CString" then your code attempts to use a wstring function "c_str()" on a type "String". Assuming you posted your code correctly you should have received a compile error which you did not post.
Perhaps someone else can divine exactly what you are attempting to do and provide you with an answer. I however require accurate specific and complete information regarding this problem before I can begin to help.
Furthermore there is no shortage of articles on the net pertaining to the variety of string types in VC++ and matters of converting between types. If you care to see what I mean just Google [MSDN CString Strings LPCTSTR TCHAR]
I highly recommend studying this subject, it will payoff huge in terms of productivity and will ease your development efforts greatly.
"No matter where you go, there your are." - Buckaroo Banzai
-pete
|
|
|
|
|
Did itg as follows 6 worked
void C_MappedTrgtFieldTab::ConvertToString(T_String & val,CString &targetVal)
{
string Target;
FromString(val,Target);
targetVal= Target.c_str();
}
Thanks anyway..
|
|
|
|
|
Hi,
How Can I convert a wstring to LPCTSTR.
I have a unicode setting for my project...
Thanks in advance...
|
|
|
|
|
Hi,
I need to wrap a native c++ class which has a virtual function this virtual function needed to be implemented with the derived classes from this class(the function is called automatically like events from the class), how can i do that using c++/cli??
thx
|
|
|
|
|
Hi, I am using managed c++ to (try) and get a project that will go through a directory of files and copy them to two directories if the contents are right. Currently the sorting goes alright, but not if I insert the copy call, well, I get this runtime error:
System.IO.IOException: The target directory already exists.
First of all, I'm hoping the directory exists... I have the code write out what it's copying from and to using Console::WriteLine, and I can assure you it is valid and proper. I tried using the overload that includes the boolean and set the boolean to true to allow it to override the file, but it still insists the directory exists.
Has anyone else noticed this and/or got a solution?
|
|
|
|
|
I feel like an idiot now
I changed the code BEFORE the call, but I didn't change the call itself to use the new variable. Now it works fine. PEBKAC right here, I'll tell ya.
|
|
|
|
|
How do I DeSerialize a file in C++ .NET (e.g. using BinaryFormatter) that was originally created by Serialized in an MFC appliction using CDocument::Serialize?
When trying to do this, I get:
BinaryFormatter Version incompatibility. Expected Version 1.0. Received Version 1684632134.539785569.
Thanks!
|
|
|
|
|
I want to setup hotkeys in my application to control vobsub to show ,hide or shift the subtitles for DivX video becuase the VobSub doesn't offer this service and it's not convenient to control it by mouse. As I know ,usual hotkey software use hook tech to implement such function, but I don't know VobSub's interface and don't know how to get it. I need your help!
|
|
|
|
|
Hi,
I have 3 projects:
"Managed C#" calling "C++ Managed Extension (mixed code)" calling "Unmanaged C++ code"
I am trying to debug them using .Net Visual Studio 2003, setting breakpoints on each project. Unfortunately, it is not able to trace down to unmanaged code.
There are some exceptions happening in unmanaged code - and I cant proceed further with the project unless I am able to debug them to know what's going on.
Has anyone come across this situation before??
cheers,
nics
|
|
|
|
|
Hi
I am in the process of upgrading an older program written using MCF.
Some parts of the new program use Managed C++ and Windows.Forms (external dll).
Is there an easy way of integrating the two - for example press a .NET button (e.g. of type System::Windows::Forms::ToolBarButton) to open a MFC dialog (of type CDialog)?
I don't want to rewrite everything as Windows.Forms (e.g. convert "CDialog" to "System::Windows::Forms::Form")
|
|
|
|
|
We are also trying to do the same and want to reuse the document view architure in one of the complex component. How do we do that? Can we make .NET form as Mainframe/ChildFrame for the views and document? Any pointers will be appreciated.
Thanks.
|
|
|
|
|