VC++ .NET 2002: Using native C++ DLLs in MC++ & Getting incomplete output

S

Scott Chang

Hi all,
I copied a set of VC++ version 6 source code of
the 'cppdll'(2 projects) from a website and put the
cppdll.cpp, cppdll.def, cpp.h, (as the 1st project) and
test.cpp (as the 2nd project) into my Microsoft VC++ .NET
2002 - Windows XP Pro PC:
//----cppdll.cpp----
#include "cppdll.h"

//prevent function name from being mangled
extern "C"
int cube(int num) {
return num * num * num;
}
//-----cppdll.def----
EXPORTS
cube
//-----cpp.h------
/*
this header is not required for this dll
to compile; but is required for the app
that will use this dll
*/

#ifndef CPPDLL_H
#define CPPDLL_H

//computes the square of the number
//prevent the function name from being mangled
extern "C"
int cube(int num);

#endif //CPPDLL_H
//------test.cpp------
#include <iostream>

#include "cppdll.h"

using namespace std;

int main() {
int num;

cout << "enter any number: ";
cin >> num;

cout << "cube of number is "
<< cube(num) << endl;

return 0;
}
===============================================
I did 'Build' successfully, clicked '! Start without
debugging' and the console had 'enter any number'
appeared. I typed in a number on the console and
pressed 'Enter' key and the console screen just
vanished!!!??? I do not know what it is wrong with my
program. Please help and tell me (1) what I should change
in the 'Configuration Properties' for each of the
cppdll.cpp, cppdll.def, cppdll.h and test.cpp files, and
(2) whether it is possible to use the native C++ input to
get the cube of the input number in the MC++ framework by
using a native C++ DLL.
Thanks in advance,
Scott Chang
 
B

Bart Jacobs

Scott,

The console window disappears when your application finishes. To keep
your application from finishing, add the following lines to the end of
the main function (before "return 0;"):

string s;
cin >> s;

This will cause the application to wait until after you have entered
some text.

Good luck!

Bart Jacobs
 
S

Scott Chang

Thanks, Bart.

I added the two statements to my test.cpp as you said.
I did 'Build' on the new test.cpp and I got the following
error:
c:\Documents and Settings\Scott H. Chang\My
Documents\Visual Studio Projects\cppdll\test\test.cpp
(17): error C2679: binary '>>' : no operator found which
takes a right-hand operand of type 'std::string' (or
there is no acceptable conversion)

I do not know what to do next to correct this error.
Please help and advise again.

Thanks,
Scott Chang
 
B

Bart Jacobs

I'm sorry for that. Instead of what I suggested before, try the following:

char buffer[200];
fgets(buffer, 200, stdin);

Also add the following to the start of the file:

#include <stdio.h>

Happy coding!

Bart Jacobs
 
S

Scott Chang

Thanks, Bart.

I followed your instructions and added your new 3 lines
of code to my test.cpp. Then I did 'Build' on the new
test.cpp. Two strange happened after I did 'Build':
1) If I clicked '! Start without Debug' or 'Start' under
the Debug menu, I got the following dialog box:
cppdll - Executable for Debugging Session
Please specify the name of the executable file to be
used for the debug session.
Executable file name:
--------------------
| |\/|
--------------------
(3 choices appear if I clicked the drop-down |\/| button):
(Internet Explorer)
(ActiveX Control Test Container)
(regsvr32)
URL where the project can be accessed (ATL Saver only):
--------------------
| |
--------------------

2) If I went to the debug folder of my C:\Documents and
Settings\Scott H. Chang\My Documents\Visual Studio
Project\cppdll\test\debug, and double clicked the
test.exe file. Then the console appeared with the "enter
any number" on the screen - if I typed in any number, the
console just vanished!!!???

I do not understand the problems 1) and 2). Please help
and give me new instructions to solve the problems.

THanks,
Scott Chang
 
B

Bart Jacobs

1) If I clicked '! Start without Debug' or 'Start' under
the Debug menu, I got the following dialog box:
cppdll - Executable for Debugging Session
Please specify the name of the executable file to be
used for the debug session.

You cannot run a DLL. You can only run an EXE. Set the EXE project as
the startup project.
2) If I went to the debug folder of my C:\Documents and
Settings\Scott H. Chang\My Documents\Visual Studio
Project\cppdll\test\debug, and double clicked the
test.exe file. Then the console appeared with the "enter
any number" on the screen - if I typed in any number, the
console just vanished!!!???

Sorry for that. I never use iostream myself. I have tested the following
solution:

int num;

cout << "enter any number: ";
string line;
getline(cin, line);
istringstream line_stream(line);
line_stream >> num;

cout << "cube of number is "
<< cube(num) << endl;

cin.get();

return 0;

You also need to include the following:

#include <iostream>
#include <string>
#include <sstream>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top