|
How can I convert managed string (System::String) to unmanaged array of chars? (char *)
--------
Dave
|
|
|
|
|
<font color=green>
#include <vcclr.h>
String* s = S"Hello";
const __wchar_t __pin * str = PtrToStringChars(s);
Cheers,
Tom Archer
Inside C#, Extending MFC Applications with the .NET Framework
It's better to listen to others than to speak, because I already know what I'm going to say anyway. - Jörgen Sigvardsson
|
|
|
|
|
Hi,
I have a Object derived class (managed) which is called 'MyObject', and I want to perform the UNmanaged method 'PostThreadMessage(UINT threadMessage, WPARAM, LPARAM)' using a 'MyObject' object as WPARAM. like this:
MyObject *pObject = new MyObject;
myThread -> PostThreadMessage(WM_MYMSG, (WPARAM)pObject, NULL);
How can I convert it?
And how to convert back ?
Thanks in advance.
|
|
|
|
|
You'll need to pass gcroot<MyObject>* instead.
|
|
|
|
|
I am trying to wrap an extensive C++ library in managed code , but I am falling over at the first hurdle. I build a simple console app as below and the first time I run it it works. I then clean the project, close it , reopen it and rebuild it and I get "void __cdecl operator delete(void*) (??@YAXPAX@Z) already defined in LIBCMTD.lib (dbgdel.obj)" . I think I have to specify a managed or unmanaged new/delete for the managed or unmanaged objects , but I cannot see how. Anyone help ?
<br />
#include "stdafx.h"<br />
#include "afxwin.h"
#include "afxext.h"
#using "mscorlib.dll"<br />
<br />
using namespace System;<br />
<br />
#pragma unmanaged<br />
class UnManaged<br />
{<br />
public:<br />
<br />
UnManaged()<br />
{<br />
}<br />
~UnManaged()<br />
{<br />
}<br />
void Show()<br />
{<br />
AfxMessageBox("Hello From the UnManaged world");<br />
}<br />
<br />
};<br />
#pragma managed<br />
public __gc class MyManaged<br />
{<br />
public:<br />
MyManaged()<br />
{<br />
m_ptr = new UnManaged;<br />
}<br />
~MyManaged()<br />
{<br />
delete m_ptr;<br />
}<br />
void Show()<br />
{<br />
m_ptr->Show();<br />
}<br />
private:<br />
UnManaged *m_ptr;<br />
};<br />
int _tmain()<br />
{<br />
Console::WriteLine("Hello Managed World");<br />
MyManaged *mg = new MyManaged;<br />
mg->Show();<br />
return 0;<br />
}<br />
<br />
>
|
|
|
|
|
Everytime I have a few SqlDataAdapters + DataSets on my form, the designer takes
over 10+ minutes to just open up the form!
What the heck is wrong with it and how do I fix this? Anyone else have this problem?
Thanks
|
|
|
|
|
Hi,
I have a simple doubt.
I've created a struct TEST like this:
_value struct TEST
{
String *str;
int cont;
};
When I try to dynamically allocate TEST like this:
TEST *pTest = new TEST;
A compilation error says to me that 'a value type object cannot be dynamically allocated with managed members on C++ (nogc) heap'
I must use the 'str' variable within this structure, so how can I dynamically allocate TEST ?
Thanks in advance.
|
|
|
|
|
As covered in Chapter 1 of my "Extending MFC Applications with the .NET Framework", this can never work because __value types are allocated dynamically in the C++ heap (free store) whereas __gc types are allocated on the CLR heap. In your example, the TEST struct is a __value type but contains a __gc embedded type. If you need TEST::str to be of type String*, then you need to make the TEST struct a __gc struct. That will compile without errors.
Cheers,
Tom Archer
* Inside C# -Second Edition
* Visual C++.NET Bible
* Extending MFC Applications with the .NET Framework
|
|
|
|
|
Sorry if i've choose not suitable thread. I can't understand exactly wich forum I need.
How can I set position of printf (or cout) output? If I've already print a half screen of a text and I want return to some used position (e.g. 10th symbol) and start to overwrite existing text what should I do? No one knows how to do this under win32 please, help.
|
|
|
|
|
If you knew you were on the same line you could output some '\b's to backspace to the appropriate place, but it doesn't around like that's the case for you. As such you need to use the Win32 Console APIs, in particular SetConsoleCursorPosition(), but they don't intergrate particularly well with standard IO.
Depending on your specific needs, you might get away with just ensuring you've flushed your output before changing the cursor position. If not, you may need to switch to using the Console API for all your output.
-Blake
|
|
|
|
|
Hi there !
I need to use a web service written in VB.NET .
i want to write a C++ client to use the service. how can
we do it in C++.
Abhi....
|
|
|
|
|
Abhi Avasthi wrote:
how can
we do it
By asking the right people. Try the Visual C++ forum.
leppie::AllocCPArticle("Zee blog"); Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.
|
|
|
|
|
Dear friend,
I want to develope a telnet Client using C++ .net but
i am biggener in C++ .net so i dont know whether any telnet
API,function or calsses in C++ .net which will help to develope telnet Client.if u know then let me know thanx in advance.
Ar if
|
|
|
|
|
Hi,
I have a managed c++ project which I've included a CWinThread derived class (CMyThread). It works fine until I call 'pMyThread -> PostThreadMessage(...)'. It occurs a exception...
Is it possible to use 'PostThreadMessage' in a managed C++ project ?
I know that it's a little weird, but I really will appreciate if someone help me.
Thanks in advance.
|
|
|
|
|
Does your thread has a message pump?
"...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..."
Me
|
|
|
|
|
I believe it does...
I fact, I took this thread class (CMyThread) from a working UNmanaged c++ project, erased all the methods and kept only one... for test purposes. I've done no extra code.
So, from the class 'MyClient' (which is a managed c++ class) I call:
pMyThread -> PostThreadMessage(SWM_TEST, NULL, NULL);
I really don't know if Managed C++ works in the same way of Unmanaged C++ when it comes to processing messages.
Please, help me.
thanks.
|
|
|
|
|
You may try Win32 ::PostThreadMessage?
Also, what you mean by 'MyClient' (which is a managed c++ class) ?
Could you post it's header?
"...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..."
Me
|
|
|
|
|
Win32 ::PostThreadMessage didn't work.
As I said before, I'm working in a Managed c++ project, so this MyClient class is a simple form class.
Within this class, I create a CMyThread object and after that I send a message to it. Like this:
MyClient::TestMethod()
{
CMyThread *pMyThread = new CMyThread();
pMyThread -> PostThreadMessage(SWM_TEST, NULL, NULL);
}
thanks.
|
|
|
|
|
MyClient::TestMethod()<br />
{<br />
CMyThread *pMyThread = new CMyThread();<br />
pMyThread -> PostThreadMessage(SWM_TEST, NULL, NULL);<br />
}
Me think, you have an obvious error:
You are forgetting to call CreateThread()...
As well as, see my comments below.
"...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..."
Me
|
|
|
|
|
I'm already calling CreateThraed() within the constructor of CMyThread...
|
|
|
|
|
Do you have any kind of sync between your main thread and CMyThread?
The only thing I could think of is that when you had CMyThread as unmanaged it was executing pretty fast without JIT involved and therefore by the time you PostThreadMessage CMyThread already had message queue. Now, when you moved CMyThread into managed JIT get's involved, more time gets spend on CMyThread creation, therefore by the time you PostThreadMessage message queue on CMyThread is not yet getting created.
Just a clue. Not strong but possible. Try to delay your main thread and see what happens.
"...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..."
Me
|
|
|
|
|
The sleep didn't work too...
I didn't type any additional code to synchronize the main thread with CMyThread, because it has already worked in the unmanaged project. Maybe you're right... with this 'JIT' involved it works in other way.
Can I manually synchronize the main thread and CMyThread ?
Sorry for my ignorance.
thanks.
|
|
|
|
|
What error are you getting?
"...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..."
Me
|
|
|
|
|
The message is like that:
Debug Assertion Failed !
Program: MyProgram.exe
File: f:\vs70builds\3077\vc\MFCATL\ship\altmfc\include\
afxwin2.inl
Line: 997
|
|
|
|
|
So, go tot this file and see what assertion is about...
Also, put a breakpoint in yours threads InitInstatnce/Run functions and see if you heat them...
"...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..."
Me
|
|
|
|