|
|
conversion from tchar* to string^
|
|
|
|
|
You have already asked this in QA. Please do not cross post.
|
|
|
|
|
I already responded to this query at Conversion from TCHAR* to string in c++[^] in Q&A. Please don't repost in multiple forums. If you have more information to add then edit your original question.
Use the best guess
|
|
|
|
|
I have an old Borland C++ project that displayed a 16 x 16 group of labels displaying 0x00 to 0xFF for the purpose of diagnostics.
I am now using MicroSoft Visual Studio 2010 C++
I am having a problem creating an array of labels at runtime that the labels are accessable by other routines such as a timer interval routine
#pragma once
#include <stdio.h>
namespace SerialTester {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
int count;
int tindex;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
count = 0;
tindex = 0;
char buffer[200];
array< Label ^ > ^ labels;
labels = gcnew array<Label^>(256);
for (int index = 0; index < 256; ++index)
{
Label ^ label = gcnew Label;
label->Size = System::Drawing::Size(25, 25);
label->Name = "C" + index;
sprintf(buffer,"%2X",index);
label->Text = Convert::ToString( index, 16 );
String^ clistr = gcnew String(buffer);
Controls->Add(label);
labels[index] = label;
}
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Panel^ panel1;
private: System::Windows::Forms::Timer^ timer1;
private: System::ComponentModel::IContainer^ components;
protected:
private:
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->label1 = (gcnew System::Windows::Forms::Label());
this->panel1 = (gcnew System::Windows::Forms::Panel());
this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
this->SuspendLayout();
this->Controls->Add(this->panel1);
this->Controls->Add(this->label1);
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void label1_Click(System::Object^ sender, System::EventArgs^ e) {
count++;
label1->Text = Convert::ToString( count, 10 );
}
private: System::Void panel1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) {
}
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e)
{
labels[tindex++]->BackColor = System::Drawing::Color::SkyBlue;
tindex%=256;
}
};
}
So when I create the form I created an array of 256 Labels at runtime, labels[x] but in the timer1_Tick I can not access them because the array is not global.
QUESTION: How can I do this, should my creation be somewheres else so that it is global.
QUESTION: in my Borland project I simply used sprintf to format the caption of each label to 00 .. FF, but can not seem to format the text simply in upper case.
Any help from anyone would be greatly appriciated.
Or a link to a tutorial on this.
Thank you in advance
Douglas
|
|
|
|
|
1. Yes you need to move the array declaration out of the constructor. As it is now, the array is a local variable to the constructor and not accessible outside of the constructor. Read up on variable scope.
2. Use the string class ToUpper() method to make the string all uppercase.
|
|
|
|
|
Hello
Thank you for your reply
I had tried declaring the following after the global int count; up at the top of the example
array< Label ^ > ^ labels;
labels = gcnew array<Label^>(256);
But I got an error saying I could declare on an unmanaged heap
Any thoughts or is this the wrong place.
My help on visual c++ does not work as nice as the c# version does
Thank you
Douglas
|
|
|
|
|
Without seeing more of what you did, it's hard to tell but it should look something like:
private array< Label ^ > ^ labels;
Form1(void)
{
}
|
|
|
|
|
Awesome
Thank you for your time that works.
Douglas
|
|
|
|
|
|
Hi There ,
I want to call a C# function from C++ , via CLI/C++.
C# code
private string _text = "";
public void setText(string text)
{
_text = "HI World";
Console.WriteLine("C# settext - {0}", _text);
}
public string getText()
{
return _text;
}
C++/CLI code
Header :
virtual void setText(std::string text);
virtual std::string getText();
CPP file
std::string CStringBridge::getText()
{
_managedObject = gcnew Bridge();
return (marshal_as<std::string>(_managedObject->getText()));
}
void CStringBridge::setText(std::string text)
{
_managedObject = gcnew Bridge();
_managedObject->setText(gcnew System::String(text.c_str()));
}
Note : When I use the following code
<pre lang="cpluscplus">virtual void setText(System::String^ text);
virtual System::String^ getText();
I get the following error 3395
__declspec(dllexport) cannot be applied to a function with the __clrcall calling convention
, and so I stuck to std::string
When I use the library from the C++/CLI code, and call from my C++ program, "Hi World" should be printed ; instead nothing gets printed
C++ console application
IStringBridgeWrapper *pBridge = IStringBridgeWrapper::CreateInstance();
pBridge->setText(std::string("I am here"));
try{
pBridge->getText();
}
catch(exception& e)
{
cout << e.what() << endl;
}
I think the string is not being properly passed .
Any ideas to solve it shall be appreicated.
|
|
|
|
|
This is how I convert a managed (c#) string to an unmanaged string (C++):
using namespace msclr::interop;
void GetUnmanagedString(String ^ managedString, char **unmanagedString)
{
marshal_context ^ context = gcnew marshal_context();
strcpy(*unmanagedString, context->marshal_as<const char *>(managedString));
delete context;
}
Note that the unmanaged string memory has already been allocated before I call this function.
|
|
|
|
|
That's just the conversion part . I don't think that's the reason for the string not getting displayed.
|
|
|
|
|
sujayg wrote: I get the following error 3395
__declspec(dllexport) cannot be applied to a function with the __clrcall calling convention Why are you trying to use dllexport in a CLI program? Make everything CLI and it should work together.
Use the best guess
|
|
|
|
|
|
That has nothing to do with it; as the message says, you cannot use dllexport on a CLI method. You are trying to mix managed and unmanaged code, which does not work well.
Use the best guess
|
|
|
|
|
What if I dont use String^ and use std::string as the preferred parameter .
|
|
|
|
|
You'll get in even more difficulty. You need to spend some time learning the difference between managed and unmanaged code in C++. I am not sure what you are trying to achieve here, but suffice it to say that what you are attempting is not straightforward. C++ was never designed to work in .NET and unless you have a compelling reason to mix the two languages you should not bother. Stick to either unmanaged C++ or C#, and make life easier for yourself.
Use the best guess
|
|
|
|
|
I want to call a C# function from C++ ; and I am using C++?CLI as the bridge .
I had developed a CLI wrapper - for calling C++ function from C# ,but this was my attempt to do the reverse.
|
|
|
|
|
|
Thanks for the link. I have created the manged DLL , without errors. I want to call this DLL from native C++ applicaton . Please help me with a step by step process how can I do it .
|
|
|
|
|
sujayg wrote: Please help me with a step by step process how can I do it . Sorry, I don't have one, you will have to figure it out from the documentation and articles on the subject. I still do not understand what you are trying to achieve here, calling managed code from unmanaged is not such a simple matter.
Use the best guess
|
|
|
|
|
It isn't clear if there is a real business problem here but mixed mode programming can often be tricky.
So it is easier to just not do it.
Instead
1. Create a executable of the targeted functionality.
2. Provide an management API for that: sockets, files or std io.
3. Use the client application to manage a "process" to run the above executable.
4. The client application uses the management api to produce the desired functionality.
The advantage to this is that both pieces, with care, can be tested independently and debugged independently as well. And the target functionality cannot take done the client application should it fail in a catastrophic way.
|
|
|
|
|
when <b>1</b> is given as input following program shows <b>b</b> as output why is that so I know that 1 given to char is manipulated differently but how it's being manipulated please explain.
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
char ch;
cout<<"enter ch ";
cin>>ch;
ch=ch*2;
cout<<ch;
getch();
return 0;
}
|
|
|
|
|
Taqi_Shah wrote: 1
Taqi_Shah wrote: ch=ch*2;
1 on the ASCII chart is 49
49 * 2 = 98
98 on the ASCII chart is b
|
|
|
|