|
Hi All
I am getting error when use command USE 12;(12 is database name)
Error is given below
Microsoft SQL-DMO (ODBC SQLState: 42000) Error: -2147221334
[Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near '12'.
Unknown error 0x800400AA
i am using code for use database is
<br />
<br />
Qry=spSQLServer->ExecuteWithResults((_bstr_t)"USE 12",(LONG)"IF EXISTS(SELECT * FROM sysdatabases WHERE name='12'".GetLength());<br />
|
|
|
|
|
I have the following native function call:
wchar_t* SXNative::ReturnString()
{
wchar_t retString[128];
wchar_t Test[] = L"This is a dummy";
wcscpy_s(retString, sizeof(Test) / sizeof(wchar_t), Test);
return retString;
}
The return value works fine from the native function.
In a C++/CLI wrapper class I have a managed type that marshalls the return type:
String^ ManagedWrapper::Managed_ReturnString()
{
wchar_t* test = nat_ptr->ReturnString();
return gcnew String(test);
}
The return type from the manaaged function returns garbage.
Any idea what is wrong with this?
Thanks in advance.
Jer 29:11
|
|
|
|
|
Al_S wrote: wchar_t* SXNative::ReturnString() {
wchar_t retString[128];
return retString;
}
IMO this isn't valid C or C++ code, it returns a pointer to a stack-based array, which is going to diappear in thin air as soon as the function returns. It may look like a good pointer on immediate inspection, but it will not work reliably.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
|
|
|
|
|
OK. It goes out of scope after it returns. How do I mend that?
Jer 29:11
modified on Tuesday, October 20, 2009 9:18 PM
|
|
|
|
|
by making sure it does not sit on the stack (the alternatives being static allocation and heap allocation).
In C, it would be a global array, or a malloc'ed buffer.
in C++ it could also be (a member of) an object, anything that gets gcnew'ed or is static.
And for anything dynamic, you would eventually have to deallocate it (e.g. call free on the thing that was malloc'ed).
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
|
|
|
|
|
Can you suggest something? I've struggled with mixed mode a bit
Jer 29:11
|
|
|
|
|
I don't know what it is you want to achieve. I guess this would be the simplest C++ code that should do it:
wchar_t* SXNative::ReturnString() {
return L"This is a dummy";
}
as now the string sits statically in memory.
If you need more C++ advice, better someone else kicks in. I answered the OP because it started as a P/Invoke issue, which it wasn't after all.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
|
|
|
|
|
Thanks. Not sure where the post was intended as P/Invoke. I was sure I was pursuing an interop question. What you posted is a portion of what I had initially. The native code worked as posted. It just goes out of scope when called my the managed function. As stated in the OP, I want to return a native string to a managed function that marshalls it to a System::String. Illustrated in the top of the thread
Jer 29:11
|
|
|
|
|
"Marshal" in the subject line got my attention...
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
|
|
|
|
|
Look at his thread ... maybe it can help, you its going from System::String (utf8 charset) do std::string
link
|
|
|
|
|
Thanks for the offer. I think I got it (see later in thread).
Jer 29:11
|
|
|
|
|
As Luc mentioned, one way to do this is to allocate the
native string and free it when you're done with it:
wchar_t* SXNative::ReturnString()
{
wchar_t* retString = new wchar_t[128];
wchar_t Test[] = L"This is a dummy";
wcscpy_s(retString, sizeof(Test) / sizeof(wchar_t), Test);
return retString;
}
String^ ManagedWrapper::Managed_ReturnString()
{
wchar_t* test = nat_ptr->ReturnString();
String^ retstr = gcnew String(test);
delete test;
return retstr;
}
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks Mark,
An illustration goes a long way than a theoretical discussion. I thought about the return type from the native code, what's been discussed and realized that rather than marshalling or converting it in the managed function, why not have the managed string object returned in native function. That's one of the bigger bonuses of C++/CLI right? So I decided to do the following:
<pre>
String^ SXNative::ReturnString()
{
wchar_t Test[] = L"This is a dummy";
msclr::auto_gcroot<String^> ret;
wcscpy_s(retString, sizeof(Test) / sizeof(wchar_t), Test);
ret = marshal_as<String^>(retString);
return ret->ToString();
}
</pre>
<pre>
String^ ManagedWrapper::Managed_ReturnString()
{
return nat_ptr->ReturnString();
}
</pre>
I want to say thanks again for posting the example because that may help someone else like it did for me. I also want to post my solution as well as I feel that it shows an alternative solution.
Thanks again Mark
Al
Jer 29:11
|
|
|
|
|
Al_S wrote: rather than marshalling or converting it in the managed function, why not have the managed string object returned in native function
I don't know what others read into your OP, but I personally
figured the native function needed to be pure native. Had I known
it could be mixed then it would have been much simpler
Cheers,
MArk
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Just the same, I would appreciate your opinion on what I posted. Both version work but I would value your input as to what I showed.
The real native code is left out for proprietary reasons, but what was illustrated in context was not necessarily written in stone as to having to be completely native code. I should have stressed that the wrapper was a CLR DLL and that mixed mode was viable.
Jer 29:11
|
|
|
|
|
Al_S wrote: I would appreciate your opinion on what I posted
It won't compile as shown?
I suppose it could be simplified...
String^ SXNative::ReturnString()
{
wchar_t Test[] = L"This is a dummy";
return gcnew String(Test);
}
or
String^ SXNative::ReturnString()
{
return gcnew String(L"This is a dummy");
}
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Mark Salsbery wrote:
It won't compile as shown? Smile
I suppose it could be simplified...
What YOU posted DOES compile and work for me. Perhaps I don't understand what you mean.
Jer 29:11
|
|
|
|
|
Al_S wrote: Perhaps I don't understand what you mean.
That was my opinion (joking) on the code you posted,
because you asked.
There's so many ways to code - I try hard not to give opinions (I'm
hardly qualified to decide what code is good or bad), but instead I
prefer to provide alternatives. Someone else may come along
and provide an even better alternative...etc.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Mark Salsbery wrote:
Someone else may come along
and provide an even better alternative...etc.
Fair enough!
My intent was just to put helpful information to add to what I received as help.
Jer 29:11
|
|
|
|
|
Al_S wrote: My intent was just to put helpful information to add to what I received as help
I know. But you also asked my opinion - I quoted you on that
(ok, you didn't "ask", but you stated you'd appreciate it heh)
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I just need to know if my approach to the problem is good or if there is a better way:
Poroblem:
i need to make an application that have 3 layer:
1) first layer contains a background image
2) second layer contains text that goes over image in trasparent way
3) third layer cotains vector like arrow...ellipse and similar things that goes over image and string
let's say like a painting multylayer application...whis is the best way to do this??
by now i'm thinking at making 3 bitmaps, and on the paint calling i draw them in sequence one after the other.
Is this the right way or there are some other way to try??
thanks in advance
|
|
|
|
|
I tend to use only one canvas (I prefer a Panel for that) and paint all the objects on it, in the right order. Transparancy in GDI/GDI+ is a mess.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
|
|
|
|
|
Good evening all,
I need some assistance invoking MFC COM objects from a C++/CLI application.
My COM's are based on "Automation of COM Dialogs in a DLL" by intensely_radioactive from this site, and were originally written in VC++ 6.0
I am reworking my calling application as a Windows Forms using C++/CLI 9.0 module
The COM call is successfull in that I get the "Logon Called" message on screen (See extracts below), but I need something in .NET to play the role of pDlg (from my MFC module) so that I can invoke DoModal in the COM object.
I would prefer not to have to modify the COM, but I appreciate suggestions around that too.
Extract from COM object's module LogonFactory.cpp
LPDISPATCH CLogonFactory::GetDialog()
{
// TODO: Add your dispatch handler code here
AfxMessageBox("Logon Called", MB_OK|MB_ICONINFORMATION);
CLogon *dlg = new CLogon();
// Set the argument to true to increase the ref count
return dlg->GetIDispatch(TRUE);
}
Extract from COM Object's module Logon.cpp
void CLogon::Logon()
{
// TODO: Add your dispatch handler code here
DoModal();
}
Extract from MFC appliction's code that invokes it:
HRESULT hrx;
hrx = CoInitialize(0);
// Use smart pointers to create an instance of the pointers
ILogonFactoryPtr pFactory;
hrx = pFactory.CreateInstance(__uuidof(LogonFactory));
ASSERT(SUCCEEDED(hrx));
if (SUCCEEDED(hrx))
{
// Get the factory to show the dialog
ILogonPtr pDlg;
pDlg = pFactory->GetDialog();
// And activate the button through COM
do
{
pDlg->Logon(); // This function contains the DoModal call in the COM
// and as such actually displays the form
}
}
Extract from C++/CLI calling module
LogonFactory^ GetLogon;
GetLogon = gcnew LogonFactory();
GetLogon->GetDialog();
Ger
|
|
|
|
|
Hi, i am new to Visual C++ and facing problem with combo boxes.
Here is what i need:
Combobox1
Choice: Metal, Plastic
Combobox2
Choice: if Metal in Combobox1: 2,3,4,5,6
Choice: if Plastic in Combobox1: 8,10,15
I am unable to write code for the event, can any one help me out with the hints or links or code!...any help is highly appreciated...!
#pragma once
namespace PR {
using namespace System; using namespace System::ComponentModel; using namespace System::Collections;
using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{ InitializeComponent();
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::ComboBox^ comboBox1;
protected:
private: System::Windows::Forms::ComboBox^ comboBox2;
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
this->comboBox2 = (gcnew System::Windows::Forms::ComboBox());
this->SuspendLayout();
this->comboBox1->FormattingEnabled = true;
this->comboBox1->Items->AddRange(gcnew cli::array< System::Object^ >(2) {L"METAL", L"PLASTIC"});
this->comboBox1->Location = System::Drawing::Point(81, 80);
this->comboBox1->Name = L"comboBox1";
this->comboBox1->Size = System::Drawing::Size(121, 21);
this->comboBox1->TabIndex = 0;
this->comboBox1->Text = L"METAL";
this->comboBox2->FormattingEnabled = true;
this->comboBox2->Items->AddRange(gcnew cli::array< System::Object^ >(5) {L"2", L"3", L"4", L"5", L"6"});
this->comboBox2->Location = System::Drawing::Point(81, 126);
this->comboBox2->Name = L"comboBox2";
this->comboBox2->Size = System::Drawing::Size(121, 21);
this->comboBox2->TabIndex = 1;
this->comboBox2->Text = L"2";
this->comboBox2->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::comboBox2_SelectedIndexChanged);
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 266);
this->Controls->Add(this->comboBox2);
this->Controls->Add(this->comboBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void comboBox2_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) {
}
};
}
|
|
|
|
|
Invinci wrote: private: System::Void comboBox2_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) {
Should'nt this be an event for comboBox1 (comboBox1_SelectedIndexChanged ) ? You need to change/update comboBox2 when comboBox1 changes.
I'm not quite familiar with the exact syntax of ManagedC++/CLI (and the .net framework, so I will only give you hints.
In the handler for comboBox1:
- You need to clear the content of comboBox2 (remove the Items in it)
- Get the currently selected value of comboBox1
- Check the selected value to see if you need to fill comboBox2 with either "2,3,4,5,6" or "8,10,15".
- Fill in the values in comboBox2.
in pseudocode:
comboBox2.clearContent();
string s = comboBox1.GetStringValue();
if ( s == "Metal")
this->comboBox2->Items->AddRange(gcnew cli::array< System::Object^ >(5) {L"2", L"3", L"4", L"5", L"6"});
else if ( s == "Platic")
this->comboBox2->Items->AddRange(gcnew cli::array< System::Object^ >(5) {L"8", L"10", L"15" });
Max.
This signature was proudly tested on animals.
|
|
|
|