473,513 Members | 2,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple managed app won't work...

Excuse me if you consider this a cross post, however, it was originally destined for this group not the vc.atl group, but somehow it went there instead of here! (obviously it was something I did, but hey ho) ... now back to the problem in hand

I've made what I consider to be the simplest of applications, but it won't work. This is my code

#using <mscorlib.dll>
#include <tchar.h>
using namespace System;

int _tmain(void)
{
String* args[] = Environment::GetCommandLineArgs();

try
{
try
{
Console::WriteLine(String::Format(S"You supplied {0} arguments", __box(args->Length)));
}
catch(Exception* e)
{
Console::WriteLine(S"\nERROR WITH args\n---------------\n");
Console::WriteLine(e->Message);
Console::WriteLine(e->TargetSite);
Console::WriteLine(e->StackTrace);
}

if(!args)
Console::WriteLine(S"\nYou didn't supply any arguments\nVielleicht das nachster zeit!\n\n");
else if(args->Length > 1)
Console::WriteLine(S"\nYou supplied too many arguments\nVielleicht das nachster zeit!\n\n");
else
{
int counter = Int32::Parse(args[0]);
for(int i = 0; i < counter; i++)
{
Console::WriteLine(S"I am a programmer.");
}
Console::WriteLine(S"");
}
}
catch(Exception* e)
{
Console::WriteLine(S"\nERROR\n-----\n");
Console::WriteLine(e->Message);
Console::WriteLine(e->TargetSite);
Console::WriteLine(e->StackTrace);
}

return 0;
}

and this is the result of running the application:

W:\C++\MTest\Debug>MTest

ERROR WITH args
---------------

Object reference not set to an instance of an object.
Int32 main()
at main() in w:\c++\mtest\mtest.cpp:line 20

ERROR
-----

Input string was not in a correct format.
Int32 ParseInt32(System.String, System.Globalization.NumberStyles, System.Globalization.Nu
mberFormatInfo)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s)
at main() in w:\c++\mtest\mtest.cpp:line 36

W:\C++\MTest\Debug>MTest 2

ERROR WITH args
---------------

Object reference not set to an instance of an object.
Int32 main()
at main() in w:\c++\mtest\mtest.cpp:line 20

You supplied too many arguments
Vielleicht das nachster zeit!

Nov 17 '05 #1
3 1171
Looks like you want an app that takes a single command-line argument (eg. 'mtest 3'), and it specifying the number of times 'I am a programmer.' is displayed.

args->length is the number of command-line parts - of which there is at least 1, the full path to the app itself (eg. 'c:\test\mtest.exe'). So change it your code to;

if(args->length < 2)
Console::WriteLine(S"\nYou didn't supply any arguments\nVielleicht das nachster zeit!\n\n");
else if(args->Length > 2)
Console::WriteLine(S"\nYou supplied too many arguments\nVielleicht das nachster zeit!\n\n");
else
{
int counter = Int32::Parse(args[1]);
for(int i = 0; i < counter; i++)
{
Console::WriteLine(S"I am a programmer.");
}
Console::WriteLine(S"");
}

"billr" wrote:
Excuse me if you consider this a cross post, however, it was originally destined for this group not the vc.atl group, but somehow it went there instead of here! (obviously it was something I did, but hey ho) ... now back to the problem in hand

I've made what I consider to be the simplest of applications, but it won't work. This is my code

#using <mscorlib.dll>
#include <tchar.h>
using namespace System;

int _tmain(void)
{
String* args[] = Environment::GetCommandLineArgs();

try
{
try
{
Console::WriteLine(String::Format(S"You supplied {0} arguments", __box(args->Length)));
}
catch(Exception* e)
{
Console::WriteLine(S"\nERROR WITH args\n---------------\n");
Console::WriteLine(e->Message);
Console::WriteLine(e->TargetSite);
Console::WriteLine(e->StackTrace);
}

if(!args)
Console::WriteLine(S"\nYou didn't supply any arguments\nVielleicht das nachster zeit!\n\n");
else if(args->Length > 1)
Console::WriteLine(S"\nYou supplied too many arguments\nVielleicht das nachster zeit!\n\n");
else
{
int counter = Int32::Parse(args[0]);
for(int i = 0; i < counter; i++)
{
Console::WriteLine(S"I am a programmer.");
}
Console::WriteLine(S"");
}
}
catch(Exception* e)
{
Console::WriteLine(S"\nERROR\n-----\n");
Console::WriteLine(e->Message);
Console::WriteLine(e->TargetSite);
Console::WriteLine(e->StackTrace);
}

return 0;
}

and this is the result of running the application:

W:\C++\MTest\Debug>MTest

ERROR WITH args
---------------

Object reference not set to an instance of an object.
Int32 main()
at main() in w:\c++\mtest\mtest.cpp:line 20

ERROR
-----

Input string was not in a correct format.
Int32 ParseInt32(System.String, System.Globalization.NumberStyles, System.Globalization.Nu
mberFormatInfo)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s)
at main() in w:\c++\mtest\mtest.cpp:line 36

W:\C++\MTest\Debug>MTest 2

ERROR WITH args
---------------

Object reference not set to an instance of an object.
Int32 main()
at main() in w:\c++\mtest\mtest.cpp:line 20

You supplied too many arguments
Vielleicht das nachster zeit!

Nov 17 '05 #2
"martink" wrote:
Looks like you want an app that takes a single command-line argument (eg. 'mtest 3'), and it specifying the number of times 'I am a programmer.' is displayed.

args->length is the number of command-line parts - of which there is at least 1, the full path to the app itself (eg. 'c:\test\mtest.exe'). So change it your code to;
that makes sense, however, this is the line throwing the first Exception
Console::WriteLine(String::Format(S"You supplied {0} arguments", __box(args->Length)));
and the Exception message is
Object reference not set to an instance of an object.


This Exception is thrown whether or not any command line args are supplied
Nov 17 '05 #3
Thank you martink, with your help I seem to have found the problem, though a little more reading is obviously required with regards to property accessors.

First off, I must adjust my array checking accordingly, to take into account the fact that the first command-line arg is the full path to the app.

Next, instead of simpy using the args->Length property I have changed the code to call the C++ accessor function. i.e. args->get_Length(), and this works a treat.

thus the fresh code looks like this ....

try
{
int len = args->get_Length() - 1;

try
{
Console::WriteLine(String::Format(S"You supplied {0} arguments", __box(len)));
}
catch(Exception* e)
{
Console::WriteLine(S"\nERROR WITH args\n---------------\n");
Console::WriteLine(e->Message);
Console::WriteLine(e->TargetSite);
Console::WriteLine(e->StackTrace);
}

if(len == 0)
Console::WriteLine(S"\nYou didn't supply any arguments\nVielleicht das nachster zeit!\n\n");
else if(len > 1)
Console::WriteLine(S"\nYou supplied too many arguments\nVielleicht das nachster zeit!\n\n");
else
{
int counter = Int32::Parse(args[1]);
for(int i = 0; i < counter; i++)
{
Console::WriteLine(String::Format(S"{0}: I am a programmer.", __box(i+1)));
}
Console::WriteLine(S"");
}
}
catch(Exception* e)
{
Console::WriteLine(S"\nERROR\n-----\n");
Console::WriteLine(e->Message);
Console::WriteLine(e->TargetSite);
Console::WriteLine(e->StackTrace);
}

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
5064
by: someone else | last post by:
hi all I'm a newbie to this group. my apologies if I break any rules. I've wrote a simple program to find the first 1,000,000 primes, and to find all primes within any range (up to 200 * 10^12) it's pretty efficient, it took 15 minutes to compute the first 1,000,000 primes.
2
1134
by: Jason | last post by:
I am porting code from c++ and would like to use managed c++ to turn it into an assembly for c#. The c++ code uses templates heavily ( via custom STL containers ) and from what I read so does managed c++. How can I expose a templated class for use in C#, or is it just an imposibility? Jason
3
1710
by: Lei Jiang | last post by:
If I have to add __gc in every class, it would be a huge work. Is there any option that could compile all C++ class into manganged class without modify any source code?
1
1071
by: Ramsey | last post by:
When I first started my project I was using stl for all the real processing and managed c++ for all the pretty forms. But after a while the project became too large to manage. Now I am thinking of breaking up the project into dlls/lib files but I am running into the following problems. My project can be logically broken into 4 groups of...
7
7380
by: Lee Crabtree | last post by:
I'm starting work on what will eventually be a very, very LARGE project. A lot of the project involves taking C/C++ class libraries and wrapping them with managed C++. I'd like to minimize the number of DLLs I have (because I'd like to keep my sanity). Is there some way to compile a managed C++ DLL into a C# DLL, or am I just stuck...
8
5857
by: Notre Poubelle | last post by:
Hello, I have some legacy unmanaged C++ code in which I've introduced some managed C++ using the new CLI feature of VS 2005. My managed C++ code invokes a number of methods on a new C#class I've written. This C# class creates additional managed objects and passes one of them back to the managed C++ code. I believe I have everything...
9
8240
by: raylopez99 | last post by:
microsoft.public.dotnet.languages.vc Please comment on the following code fragment. Note the comments "Why"? Particularly, (1) why does referencing and dereferencing a pointer give the same thing (at least in WriteLine(), which might be some sort of cast going on behind the scenes), (2) why doesn't the pointer "stick to" a reference,...
20
2271
by: =?Utf-8?B?VGhlTWFkSGF0dGVy?= | last post by:
Sorry to bring up a topic that is just flogging a dead horse.... but... On the topic of memory management.... I am doing some file parcing that has to be done as quick as posible, but what I have found hasnt been encouraging... I found that the *quickest* way is to use a struct to retrieve fixed length packets out of the file, but structs...
9
3529
by: =?Utf-8?B?RWR3YXJkUw==?= | last post by:
I would greatly appreciate some help on passing managed object into unmanaged code. I need to pass a reference (address of) of a managed class into unmanaged code (written by a thrid party). The 3rd party unmanaged DLL will pass this reference into standard Win32 unmanaged static callback function in my code. Inside this unmanaged callback...
0
7270
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
5103
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4759
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3255
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
473
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.