|
Well, you could make the classes managed for use in .NET but I'm not sure if that is what you want.
"We will thrive in the new environment, leaping across space and time, everywhere and nowhere, like air or radiation, redundant, self-replicating, and always evolving." -unspecified individual
|
|
|
|
|
well, I want it to be accessible from .NET apps as well. My familiarity with .NET is limited - what is the mechanism that I can use, if I want it to be accessible from .NET, VC++, VB etc...
I expect people to be using .NET, VB or VC++. I know COM can be used. I wanted to be sure if there is a better option avaiable.
The application is huge, and porting it to a managed one is ruled out atleast at present.
My article on a reference-counted smart pointer that supports polymorphic objects and raw pointers
modified 29-Aug-18 21:01pm.
|
|
|
|
|
To make the classes managed for use in .NET you just need to instert __gc in the class declaration. For example:
<font color="blue">public __gc class </font>yourClass
{
};
The only downside to this that I just remembered is that you wont be able to use the managed .NET class from a unmanaged program. So I don't think this would be the best option for you.
"We will thrive in the new environment, leaping across space and time, everywhere and nowhere, like air or radiation, redundant, self-replicating, and always evolving." -unspecified individual
|
|
|
|
|
Use COM.
It can run without users need to install the .NET framework.
It can be accessed from VC++ and VB
It can also be accessed from .NET-languages, because they are compatible with COM (interop)
You don't have to make a lot of managed C++ code in your existing native application.
Just my 2 cents...
- Anders
Money talks, but all mine ever says is "Goodbye!"
|
|
|
|
|
|
This is probably a dumb question, but I am a newbie:
I am trying to catch error if an attemp is made to read past the end of an array.
For example I have an array double fVector[10];
Then I have the following code:
try {
for( int n = 0; n < 50; n++ ) {
a[n] = fVector[n];
}
} catch( int ) {
printf("Error!!!");
}
The problem may (and sometimes may not) result if I try to access element #11. But how do I catch that? If I knew the length of fVector, I could add something like this:
if( n > nLength ) throw 1;
but what if I don't know it? How can I trap the error before the application crashes with an invalid memory address error?
Thanks
|
|
|
|
|
There is no guarrantee that an access violation exception will be thrown when you read over the end of an array like that. The address beyond the end of the array may still be valid (i.e. it may contain another variable). Instead, use std::vector, and use the at() method to retrieve values. The at() method checks the array bounds, and throws an out_of_range exception if the bounds are exceeded. Alternatively, std::vector also provides the size() method, so you can do the range checking yourself, and then access the elements like a normal array using operator[].
Dave
|
|
|
|
|
Thanks for your reply.
I know there is no guarantee that the application will throw an exception; however, I would like to catch it (if it's thrown) to avoid the termination of an application.
The reason why I want to stick with an array of doubles (as opposed to using a different class) because the data will be supplied in this format into a DLL by a user.
|
|
|
|
|
If the user is passing an array of double 's into your DLL, you should have them pass the number of values in the array as well. That's the only way to ensure that you don't run off the end of the array.
Software Zen: delete this;
|
|
|
|
|
That's what I am doing right now; however, users can be dumb enough to supply an array of 10 values but set the number of attached values to 20.
I guess I can rephrase the question a little bit:
Is there a way to trap UNEXPECTED error in C++? As far as I can see, you can stick in "throw" where you think the application may fail, but is there a way to assign error handling to the whole routine so that no matter where and why it fails there, go to a specific location. For example in VB, it is achieved this way:
On Error GoTo ErrorHandler
....
code
.........
Exit Function
Error Handler:
if err = 12 Then .....
|
|
|
|
|
That IS what exceptions are for in the first place.
You wrap the part you want into try{..} and catch the errors afterwards. There your routine can ask user for help, rectify things and retry or just return.
OR you can write simply throw; and catch one level higher.
The idea is not to brach on some magic numbers, but on the type of the exception object thrown - you can have a whole battery of catch-handlers after one try, and if you oder them from derived to base, you can fine tune what you catch.
try
{
..
}
catch (const std::myFunny_error& e)
{
..
}
catch (const std::runtime_error& e)
{
..
}
catch (const std::exception& e)
{
..
}
catch (...)
{
}
My opinions may have changed, but not the fact that I am right.
|
|
|
|
|
Try looking up "structured exception handling" in the MSDN. From what I can see, this lets you catch the access violation.
Another way to get around this problem would be to modify your library interface. Suppose your current routine is like this:
void OperateOnArray(int array[], int array_size);
You could change the interface to something like the following:
void StartOperation();
void SetOperationItem(int index,int item);
void DoOperation()
In this case, the array is managed by your library. StartOperation(); initializes the array to the empty condition. SetOperationItem() sets an item in the array, growing it as necessary. Finally, DoOperation() does the operation the original function OperateOnArray(...) did, except it does it on the array your code is managing. If the caller needs to get the array back as part of the results, you could add a corresponding function like:
void GetOperationResult(int index,int *result);
Software Zen: delete this;
|
|
|
|
|
you might also look at IsBadReadPtr and IsBadWritePtr. as the other poster said, it doesn't stop you from running off the end of the array into other legal, but unrelated, chunks of memory; but it will tell you if you're on your way to an access violation or not.
-c
Be very, very careful what you put into that head, because you will never, ever get it out. --Thomas Cardinal Wolsey
|
|
|
|
|
Use std::vector instead of plain arrays, and at() instead of [] .
|
|
|
|
|
It's all nice of you to suggest using vector class instead, but how can I use it to pass a list of items from a VB application into a C++ DLL?
With an array of doubles it's possible and easy ...
|
|
|
|
|
std::vector < double > vec;
..fill vec..
Now &vec[0] is a pointer to an array of vec.size() doubles, that you can pass into dlls, or hand over to VB or whatever. You just cant extend or shrink it on the fly. For this you need to use the vector-interface.
My opinions may have changed, but not the fact that I am right.
|
|
|
|
|
What about the other way around: from VB to C++?
|
|
|
|
|
I have no idea about VB.
But you said your caller gives a number of items and where they are stored, so that you can copy them into a C-Array.
So, simply use resize() to alloate enough space in your std::vector , and copy to the address &vec[0].
My opinions may have changed, but not the fact that I am right.
|
|
|
|
|
in this case, you should probably use an actual vector, as in the STL. Then you'd have no problems at all.
Christian
No offense, but I don't really want to encourage the creation of another VB developer.
- Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael
P Butler 05-12-2002
It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003
|
|
|
|
|
I´m working on a backup service for our customers. Som backup software has an open files option to backup file left opened when the office is left.
How to do that any idea !
|
|
|
|
|
I have rather wide experience, programming using MFC5 (MFC42.dll) with MS VC++ 6.0. Recently I obtained VS .NET. I've made MFC-application, link MFC as static library and run the app on Win98se. Here I was notified about absence of OLEACC.dll although I did not check to use Accessibility API in MFC app wizard. Then I have made a setup project for my app. It included OLEACC.dll and other required files. This time win98se said that it couldn’t run OLEACC.dll, and ask me to check for contents of this library.
Someone ever met such a problem. Is there any way to build my MFC projects with .Net and run them on Win9x/NT/XP? Or I should use VC6 as before?
|
|
|
|
|
For some reason, VC7 apps seem to always link to oleacc.dll. The way around it is to delay loading it (have it load only if and when required).
First you have to link to delayimp.lib.
#pragma comment (lib, "delayimp.lib")
Then set your app settings
Project => Properties => Linker => Advanced => Delay loaded Dll => Support...
Project => Properties => Linker => Input => Delay loaded DLLs => "oleacc.dll"
Sonork 100.11743 Chicken Little
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
Within you lies the power for good - Use it!
|
|
|
|
|
I've never had this problem and I've built many programs with VC++7 MFC using VS.NET. All have worked on 98/ME/NT/2000/XP with one exception: A CFileDialog box didn't work properly on a 98 machine.
Art
|
|
|
|
|
Is there a quick and easy way to insert a new element into an existing CString Array? I'd like to know if there is a function to do this or if I just need to set up a FOR loop.
If I have to use a FOR loop, how do I trap on exceeding the size of the Array?
Thanks!
John Hagen
|
|
|
|
|
Use the CStringArray instead, it provides some handy functions like InsertAt,Add,SetAt,etc..
regards
modified 12-Sep-18 21:01pm.
|
|
|
|