|
John Robert Wilk wrote: My question is can memory allocated with gcnew be deallocated before the garbage collector deallocates it using the delete operator No. delete on a managed object won't remove the instance from CLI heap. All it does is call the destructor of the object. GC will reclaim the memory later.
You can cleanup the resources that you have used, like connection object or any other native handles deterministically. But the object will be deallocated only by garbage collector. On a garbage collected environment, you don't have to worry about cleaning up the memory allocated. All you have to ensure is no strong references exists to the object that will prevent GC from collecting.
For cleaning the resources, C++/CLI provides methods like "Stack semantics". In which the compiler implements IDisposable on the type and create code to call Dispose() method when the scope ends. You can write your cleanup code inside the Dispose() method. This is the recommended way of cleaning up managed objects. For native objects, you have to call delete on them.
John Robert Wilk wrote: The problem is that the database connection is made in the constructor of the database class Usual pattern for database access is, opening connection, executing query and closing the connection. ADO.NET has connection pooling built in and connection instances are created from there. Unless you have a good reason for initializing the connection on the constructor, the pattern explained should be followed.
Best wishes,
Navaneeth
|
|
|
|
|
|
Hi!
Is there an equivalent for MSXML.XMLHTTPRequest(VB) in C++/CLI? I've to send an XML Text to a server. How to do this using C++/CLI?
|
|
|
|
|
You can use the WebRequest[^] class.
Best wishes,
Navaneeth
|
|
|
|
|
Hi!
I used the following code. It only prints "OK". But I want to send an XML text to the server and also downlod from the server. This is the code:
<br />
WebRequest^ request = WebRequest::Create( "https://live.play368.com/cgibin/EClientIntegration" );<br />
request->Credentials = CredentialCache::DefaultCredentials;<br />
HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());<br />
Console::WriteLine( response->StatusDescription );<br />
System::String^ Dsc = response->StatusDescription;<br />
Stream^ dataStream = response->GetResponseStream();<br />
StreamReader^ reader = gcnew StreamReader( dataStream );<br />
String^ responseFromServer = reader->ReadToEnd();<br />
Console::WriteLine( responseFromServer );<br />
reader->Close();<br />
dataStream->Close();<br />
response->Close();<br />
What else need to be done to send XML text to the server and download from server?
|
|
|
|
|
Hi
If i use /clr mode to compile a code that has somthing like the following:
int x = 3;
char ch='A';
int arr[]="Hi";
array<int>^ ManArr1={44};
array<int>^ ManArr2= gcnew array<int> {44};
my questions now:
Would the type int be mapped to System::Int32 ?? and what about char ch ? Are they considerd as native or managed type? Where will be executed! through MSIL or not!!
We see that int arr[] is a native array, does that mean it will be executed out of MSIL?
The last question ,, For both the managed array ManArr1 & ManArr2 what is the difference between the two initialization ??
|
|
|
|
|
Good question!
anti.AS wrote: Would the type int be mapped to System::Int32
Yes. By default compiler treat it as managed integer. And what type of interger is implementation defined. On a 32 bit machine you could get System.Int32 and Int64 on 64 bit machines. Now consider the following example
int x = 3;
std::cout << x; std::cout expects a native integer and you are passing a managed one. So the compiler does the conversion for you.
anti.AS wrote: and what about char ch ?
This will be compiled as System.SByte .
anti.AS wrote: We see that int arr[] is a native array
int arr[]="Hi"; is an invalid statement. But in general, this is treated as a managed CLI array.
anti.AS wrote: The last question Wink ,, For both the managed array ManArr1 & ManArr2 what is the difference between the two initialization ??
Nothing. First one is a syntactic sugar.
Hope that helps
Best wishes,
Navaneeth
|
|
|
|
|
Thank u N a v a n e e t h... great information ...
btw the native array will be considered as unverifiable unlike the managed one.
|
|
|
|
|
Hi!
My XML file contains the following:
<?xml version="1.0" encoding="UTF-8"?>
<download>
<English>
<Updated_date>16/11/2010</Updated_date>
<Filename>Update_Eng</Filename>
</English>
<Updateexe>
<Updated_date>16/11/2010</Updated_date>
<Filename>Update</Filename>
</Updateexe>
</download>
I've to read from the program(C++/CLI), the value of the tag <Updated_date> which is inside the tag <Updateexe> . How to do this in C++/CLI?
|
|
|
|
|
Use XPathNavigator to select the node(s) you're interested in.
Example:
XPathDocument^ document = gcnew XPathDocument("C:\\temp\\test.xml");
XPathNavigator^ navigator = document->CreateNavigator();
XPathNodeIterator^ nodes = navigator->Select("*/Updateexe/Updated_date");
while (nodes->MoveNext())
{
Console::WriteLine(nodes->Current->ToString());
}
John
|
|
|
|
|
Hi, I need to use a generic and they're obviously different than C++ templates but I'm a bit stumped. Do you know if there's a better way to do this?
Imagine a base class "MyWindowBase" and a bunch of derived classes. I need to grab instances of the template type from a master list.
generic<class WindowType> where WindowType : MyWindowBase
System::Collections::IList^ WindowManager::getAllWindowsOfType()
{
List<WindowType>^ results = gcnew List<WindowType>();
for each (WindowBase^ w in mWindowList) {
try {
WindowType asWT = cli::safe_cast<WindowType>(w);
results->Add(asWT);
}
catch (System::InvalidCastException^)
{}
}
return results;
}
Example usage:
for each(ReportWindow^ report in WindowManager::Instance->getAllWindowsOfType<ReportWindow^>())
{
}
This works but the safe_cast has a smell because it throws an exception if the cast is not allowed.
I'd prefer to use dynamic_cast instead with a nullptr check but that doesn't seem to be allowed. Could I somehow check the typeid instead? Still feeling my way around these generics a bit.
John
|
|
|
|
|
|
Thanks Philippe! You've confirmed my suspicion and thanks for the stackoverflow link - don't know why that didn't show up in my searches...
John
|
|
|
|
|
Hi!
I've to write code to establish a client/server communication in C++. What are the steps involved? Can any body give me some sample code?
|
|
|
|
|
Please pick ONE forum to ask your question. Posting the same question in more than one place only annoys people and makes them less likely to help you.
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
Hi All,
Every body know this question answer please send me the reply?
In visual studio 6.0 .Debugging operation performed press F10 it takes to the next step into host information(means takes assembley) in windows 7 64-bit environment.
Thanks.
|
|
|
|
|
Hi!
I want the advice of the community on using usual static_cast instead of cli::safe_cast. Which one is preferable?
|
|
|
|
|
As far as I know, safe_cast is more like dynamic_cast (which you can also use on managed types). safe_cast and dynamic_cast are for casting between handles to polymorphic types within a class hierarchy (much as you would do in C++ with pointers). The difference is that safe_cast throws an exception when the cast is not possible rather than returning nullptr. This means you don't have to write code that checks to see whether the cast succeded, just handle the exception, meaning potentially less cluttered code.
|
|
|
|
|
Just wondering how and if you can pre-select multiple files as the dialog opens? Do you place a list of them in the Initial Filename area, as I've tried this already the answer seems to be, No.
Am I missing something?
Basically I'd like a user to be able to select multiple files, I then do some checking of the filenames and if one of these checks comes back false, I want to reload this dialog with the files that the user had previously selected.
Any help would be greatly appreciated
Hayden
CFileDialog dlg(FALSE,NULL,"",OFN_ENABLESIZING|OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_ALLOWMULTISELECT,"All Files(*.*)|*.*||");
const int nBufferSize = 128*1024;
TCHAR *szNewBuffer = new TCHAR[nBufferSize];
memset(szNewBuffer, 0, sizeof(TCHAR) * nBufferSize);
dlg.m_ofn.lpstrFile = szNewBuffer;
dlg.m_ofn.nMaxFile = nBufferSize - 1;
if (dlg.DoModal() == IDOK)
{
POSITION pos = dlg.GetStartPosition();
CString fullpath;
CString filenametemp;
CString filename;
int nLastSlashPos;
filePaths = "";
fileNameList = "";
while (pos)
{
fullpath = dlg.GetNextPathName(pos);
nLastSlashPos = fullpath.ReverseFind('\\');
if(nLastSlashPos >= 0)
{
filename = fullpath.Mid(nLastSlashPos+1);
}
if (filePaths != "")
{
filePaths = filePaths + "|";
fileNameList = fileNameList + " ";
}
filePaths = filePaths + fullpath;
fileNameList = fileNameList + '"' + filename + '"';
}
}
|
|
|
|
|
Hi!
I've a file called test.ini. The contents of this file is:
PreviousCheck =true
LID =ratha
PWD =rtrrules
I've to read the file and assign each value after the equal(=) sign to three System::String^ variables. I don't want the text preceding the equal sign(i.e PreviousCheck =,LID = and PWD = all I don't want). How to do this in C++/CLI?
|
|
|
|
|
Firstly, what you are doing is better suited to the app.config key/value approach. But to answer the question investigate the 'Split' method on the String^ class in C++/CLI to achieve what you want.
Ger
|
|
|
|
|
Hi!
I used the following code to get a line from the file(and then I will split it into parts).
System::IO::StreamReader reader("test.ini", System::IO::FileMode::Open);
System::String^ line = reader.ReadLine();
But I got the following error:
error C2664: 'System::IO::StreamReader::StreamReader(System::IO::Stream ^,bool)' : cannot convert parameter 1 from 'const char [17]' to 'System::IO::Stream ^'
Reason: cannot convert from 'const char *' to 'System::IO::Stream ^'
No user-defined-conversion operator available, or
Cannot convert an unmanaged type to a managed type
How to get rid of this error?
|
|
|
|
|
I suggest putting "Test.INI" in a variable defined as String^ e.g.
String^ FileName;
FileName = String::Format("Test.INI");
Finally when you are splitting your input strings define the index character as wchar_t
Ger
|
|
|
|
|
T.RATHA KRISHNAN wrote: How to get rid of this error?
I think this is a subtle one; you have provided a char* in parameter 1 of the constructor, but no such constructor exists. You need to use a String object as the path parameter for the compiler to select the correct constructor. You can find all the gory details here[^] on MSDN.
I must get a clever new signature for 2011.
|
|
|
|
|
This seems to work:
#include "stdafx.h"
using namespace System;
int main(array<String ^> ^args)
{
array<String^>^ lines = System::IO::File::ReadAllLines("C:\\temp\\test.ini", System::Text::Encoding::UTF8);
Console::WriteLine("Key\t\tValue");
for each (String^ line in lines)
{
if (String::IsNullOrWhiteSpace(line))
continue;
int offset = line->LastIndexOf("=");
if (offset >= 0)
{
String^ key = line->Substring(0, offset)->Trim();
String^ value = line->Substring(offset+1)->Trim();
Console::WriteLine(key + "\t\t" + value);
}
}
return 0;
}
John
|
|
|
|