|
Its working. The problem was with compiler params lib was compiled as ansi C and my projekt is in VC++ . After compiling lib in c++ compiler everything is OK
Pain is a weakness living the body
|
|
|
|
|
My ERROR:
sim50avc error LNK2001: unresolved external symbol "void __cdecl showme(int)" (?showme@@$$J0YAXH@Z)
I have created a simple Form project and tried to mix C++ and my existing C code. With problems of course. Here is the important code.
/* BEGIN - don.c */
/* ############################################################ */
/* ### This just a cut-down example of my C code ### */
/* ############################################################ */
/* ### The fact that it is C code and not C++ cannot change ### */
/* ############################################################ */
#include
extern void showme(int); /* call this routine in Form1.h */
int magiccalc() /* Magic calculation - written in C */
{
showme(3); /* use the integer 3 instead of showing the calculations code */
return(0);
}
/* ############################################################ */
/* END - don.c */
/* BEGIN - Form1.h */
#pragma once
extern "C" int magiccalc(void);
namespace sim50avc
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
///
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
///
public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
int num;
InitializeComponent();
num = magiccalc(); /* Call magiccalc in don.c */
/* num is a meaningless return value */
}
protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::TextBox * inputBox;
private: System::Windows::Forms::TextBox * outputBox;
private:
///
/// Required designer variable.
///
System::ComponentModel::Container * components;
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
void InitializeComponent(void)
{
this->inputBox = new System::Windows::Forms::TextBox();
this->outputBox = new System::Windows::Forms::TextBox();
this->SuspendLayout();
//
// inputBox
//
this->inputBox->Location = System::Drawing::Point(16, 24);
this->inputBox->Multiline = true;
this->inputBox->Name = S"inputBox";
this->inputBox->Size = System::Drawing::Size(256, 88);
this->inputBox->TabIndex = 1;
this->inputBox->Text = S"inputBox";
//
// outputBox
//
this->outputBox->Location = System::Drawing::Point(16, 184);
this->outputBox->Multiline = true;
this->outputBox->Name = S"outputBox";
this->outputBox->Size = System::Drawing::Size(256, 88);
this->outputBox->TabIndex = 2;
this->outputBox->Text = S"";
//
// Form1
//
this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->ClientSize = System::Drawing::Size(292, 302);
this->Controls->Add(this->outputBox);
this->Controls->Add(this->inputBox);
this->Name = S"Form1";
this->Text = S"Form1";
this->ResumeLayout(false);
}
public:
void __cdecl showme(int thenum)
{
MessageBox::Show(S"abc", S"Result...");
}
};
}
/* END - Form1.h */
Thank You in advance
Don
P.S. - I can send the project in zipped form if anyone wants to take a look.
|
|
|
|
|
Refer to Interoperating with Unmanaged Code in the MSDN for more information.
For one, you're mixing Managed C++ with C. If you want to use .NET objects from C, you must expose the .NET objects as COM objects first. You can then call into COM objects using the standard COM API using C++. It is possible to use C, but it's more headache than it's worth.
What is it you're trying to do here?
If you're just trying to show the result from magiccalc , just return the value from the C code without displaying anything:
int magiccalc()
{
return someResult;
}
extern __cdecl int magiccalc();
public:
Form1()
{
InitializeComponent();
int num = magiccalc();
}
Of course, if the magiccalc or other routine(s) are in an external DLL, use them via Consuming Unmanaged DLL Functions.
|
|
|
|
|
In a nutshell, I want to display data computed by my current code on a windowed form. My computations are produced by routines in maybe 50 or so "C compatible" source modules.
|
|
|
|
|
Then just call into and retrieve the computation results from the C modules from C++.NET, and display the results in C++.NET as stated above. No need to "retrofit" the C. Just make sure the C prototypes are visible to the C++ portions and away you go.
|
|
|
|
|
That should be the case but I am getting a link-time error from the C object module that SHOWME (called from C but coded as function in cpp) is an unresolved external.
public:
void __cdecl showme(int thenum)
{
MessageBox::Show(S"abc", S"Result...");
}
Should I have declared the function differently so that C can see it?
I appreciate any help you can give.
Don
|
|
|
|
|
First, that showme should be in your C++ and doesn't need the __cdecl . Second, you have to declare the prototype for the C function you'll be calling to get that int result from.
|
|
|
|
|
I've gotten past this problem.
Thanks to all
Don
|
|
|
|
|
Hi all,
I have created a DLL from some C++ code (to call from my C# app).
The DLL creates and writes to a couple of files.
However, it writes them to the folder last used by the C# app.
1. Why? (Less important)
2. How do I specify where to write the file, relative to the DLL file. (More Important)
Thanks in advance for any suggestions/hints.
J
---
|
|
|
|
|
It'll write to a relative path to the EntryAssembly's Working Folder. To find out what that is, use Environment.CurrentDirectory Property. If you want the path relative to the DLL itself (excuse the C# throughout, it was faster to type -- the C++ is very similar):
string baseDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
If you have a bare file name/relative path, relativeFileName , you can construct a full path to the file with the above baseDirectory thus:
string filePath = Path.Combine(baseDirectory, relativeFileName);
|
|
|
|
|
Thanks Ian,
I'm just setting the directory using
Environment.CurrentDirectory and string filePath = Path.Combine(baseDirectory, relativeFileName);
before I run the DLL - saves me from editing the DLL, and is easier to keep track of.
Thanks again, J
---
|
|
|
|
|
I need to implement a timer, so I copied the example from the help screen, and pasted it into a new .net console application. When I try to build it I get the following error message:
.....\mytimer.cpp(22): error C2061: syntax error : identifier 'ElapsedEventArgs'
What am I missing???
// This is the main project file for VC++ application project
// generated using an Application Wizard.
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
// the following is from sdk help
__gc class Timer1
{
public:
static void Main() {
System::Timers::Timer* aTimer = new System::Timers::Timer;
aTimer->Elapsed += new ElapsedEventHandler(0, Timer1::OnTimedEvent);
// Set the Interval to 5 seconds.
aTimer->Interval=5000;
aTimer->Enabled=true;
}
private:
// Specify what you want to happen when the Elapsed event is raised.
static void OnTimedEvent(Object* source, ElapsedEventArgs* e)
{
Console::WriteLine(S"Hello World!");
// aTimer->Enabled=true;
}
};
int _tmain()
{
Timer1::Main();
Console::WriteLine(S"Press \'q\' to quit the sample.");
while(Console::Read()!='q');
return 0;
}
|
|
|
|
|
on the line with #using..., the code has #using mscorlib.dll, but somehow didn't paste correct...
|
|
|
|
|
Hello guys,
I'm a newbee in .NET programming. Haven spent the last 4 months working with C#.NET. I want to have a feel of Managed C++ like the big boys do. Links to "very good" free e-books will be highly appreciated.
Thanks 4 always being there sirs.
...the mind is not a vessel to be filled but a fire to ignited
|
|
|
|
|
try to use eMule and search for Thinking in C++. There are few pdfs available. Autor was invertor of C++.
Pain is a weakness living the body
|
|
|
|
|
How to write the blinking word in colour?
for e.g Invalid Password in yellow bold + blinking.
thanks
|
|
|
|
|
Setup a timer and enable it if the password is invalid -- and when it's Tick event fires, change the foreground color appropriately
|
|
|
|
|
how to write code like that? i have no idea what to write...
|
|
|
|
|
Check out the System.Windows.Forms.Timer class.
Basically, you'd set up a handler for the Tick event to do the foreground color switching for you.
Set a decent Interval, like 1000 for one second, on the Timer.
When you need to blink, call Start. When you're done blinking, call Stop.
|
|
|
|
|
I would like to have a function return a reference to a managed array. I would also like to be able to supply it as a parameter to another function. Is this possible? Something like this:
<code>
int anArray __gc[];
int getArray () __gc[];
int getArray () __gc[]
{
return anArray;
}
void processArray( int arrayRef __gc[] )
{
// do something to anArray through arrayRef
}
...
processArray( getArray() );
</code>
|
|
|
|
|
Ok, here is something more specific...
I have a class, class1, that contains 2 arrays. Let's call them array1 and array2. I am instantiating 3 objects of another class, class2, from class1. To the first two, I am passing array1 to the constructor, and to the third, I am passing array2. I would like to have these three new objects hold a reference to the arrays in the main class instead of holding their own copy. So in the constructor I'd like to do something like:
Class2( int arrayRef __gc[] ) {
this->arrayRef= &arrayRef;
}
or maybe:
Class2( int (*arrayRef) __gc[] ) {
this->arrayRef= arrayRef;
}
Either way, when I try to declare an array pointer in the header file of class2 like:
int (*arrayRef) __gc[];
I get the following compile error:
"cannot declare interior __gc pointer or reference as a member of class2"
So, I'm thinking it is not even possible to have a class hold a private reference to an array of another class. Is there some way to do this?
|
|
|
|
|
If you want to have an array of integers in a managed class, you would normally do something like this:
__gc class MyClass
{
private:
int myArray[];
public:
int* getArray()
{
return myArray;
}
};
You don't need to specify everything as __gc. If your class is already managed, its members are managed automatically.
|
|
|
|
|
hi there,
just a simple, maybe stupid, question:
is there a way i can convert a managed C++ project to an unmanaged /native one? like making something using Windows.Forms and then converting it to the winapi form? i guess there isnt as the managed project as to call the methods from assemblies, but im asking anyway.
the reason for this is that building managed applications is great as it reduces the production time and increases produtivity, but not everybody as the .NET framework installed so its hard to distribuit the application.
thanks a lot, and sorry for the poor english.
|
|
|
|
|
Hi there!
I would like to make a dll that will make the bridge with an traditional dll. The idea is to produce a new object (managed one) that makes the intrepretation from .net to old vc++ and back to .net.
I'm completly new to this things, so I would like to know where can I look to see samples, or to get the start knowledge to do this task.
Thanks in advance.
António
|
|
|
|
|
Check out Consuming Unmanaged DLL Functions.
Basically, you'd write a managed wrapper to the unmanaged old VC++ dll, which calls the unmanaged functions via Interop on behalf of the managed code consuming your wrapper.
|
|
|
|