|
Hey, just wondering, but could you make this into a C++ thing too, cause I really need it in C++
thanks
http://destinationoakdale.tripod.com/
|
|
|
|
|
Hello,
I've a problem, I've created a windows form, into this project i must use a C code (that run perfectly in visula studio) that create some trees using a matrix that is generated by the c# code, how can I make it? I must create a DLL with C code and pass it the matrix?
Thanks.
Ciao, Andrea.
|
|
|
|
|
Hello Andrea,
it's the best way to build a Class Library (like my ConsoleLibrary ) as a new project in your projectgroup.
You have to add to your C# Project a new project (File->Add Project->New Project...)
Choose Visual C++-Projects -> Class Library (.NET) and give a name (e.g. MyCLibrary)
Open project-properties of MyClibrary. Add Linker-Input LIBC.LIB for all configurations (Debug and Release).
(The default entry was "nochkclr.obj mscoree.lib")
Open then Stdafx.h file and add the line:
#include "windows.h"
Open the MyCLibrary.h file and add your c-Functions.
Make them public. Here is a example:
<br />
<br />
<br />
#pragma once<br />
<br />
using namespace System;<br />
<br />
#include <conio.h><br />
<br />
<br />
namespace MyCLibrary<br />
{<br />
public __gc class Class1<br />
{<br />
public:<br />
static int SumTest(int a, int b)<br />
{<br />
int sum = a + b;<br />
return sum;<br />
}<br />
<br />
static void Sleep(int dwMiliseconds)<br />
{<br />
::Sleep(dwMiliseconds);<br />
}<br />
};<br />
}<br />
Create the library.
Change to your C# project.
Add reference, choose projects and then MyClibrary
Open your cs file
Add the line:
using MyCLibrary;
Now you can use your c-functions:
<br />
private void button1_Click(object sender, System.EventArgs e)<br />
{<br />
int sum = Class1.SumTest(1,2);<br />
}<br />
Have fun.
|
|
|
|
|
Hello ManiB
Fisrt of all I want to say Thanks to you to help me, I have applied your instructions, but if I put in MyCLibrary a simple C code like you function the pogram run with no problem, but if I put my C code that usually with no problems in visual studio when I try to run it Visual studio say me that there errors, it make a problem whit all function that I declared in my C code and it say that "Local function definitions are illegal". Do you have an idea about this problem?
Thanks, Andrea.
|
|
|
|
|
remember the #includes!
can you post your c-code and the error you got?
|
|
|
|
|
This is the code:
-------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*CREO LA STRUTTURA DATI ALLA BASE DELL'ALBERO*/
struct elemento
{
unsigned int x, y;
char tipo;
float peso;
struct elemento *dad, *son_s, *son_c, *son_d;
};
/*USO UN TYPEDEF PER COMODITA'*/
struct elemento *root;
struct elemento *now;
struct elemento *dad;
int Mat[5][5]={{0,0,0,0,0},{0,0,0,0,0},{1,1,1,1,1},{0,0,0,0,0},{0,0,0,0,0}};
/*FUNZIONI DA REALIZZARE*/
struct elemento *create_root(int i, int j); /*creazione root*/
void print_node(struct elemento *now); /*visualizza il contenuto di un nodo*/
struct elemento *aggiungi_figlio_s(int i, int j,struct elemento *now);
struct elemento *aggiungi_figlio_c(int i, int j,struct elemento *now);
struct elemento *aggiungi_figlio_d(int i, int j,struct elemento *now);
void trova_figlio(int i, int j,struct elemento *now); /*trova i figli data la radice*/
void percorri_albero(struct elemento *now, int Im, int v, int mat_v[3][5], float arr_v[5]);
void stampa_prodotto(float arr_v[5], float P,int i);
/*creazione root*/
struct elemento *create_root(int i, int j)
{
struct elemento *temp;
if((temp=(struct elemento *)malloc(sizeof(struct elemento)))==NULL)
{
printf("\nErrore creazione della radice");
return NULL;
}
temp->tipo='r';
temp->x=i;
temp->y=j;
temp->peso=1;
temp->dad=NULL;
temp->son_s=NULL;
temp->son_c=NULL;
temp->son_d=NULL;
return temp;
}
/*visualizza il contenuto di un nodo*/
void print_node(struct elemento*now)
{
printf("\n***\n\tTipo: %c\n\tX: %d\n\tY: %d\n\tPeso: %f\n***",now->tipo,now->x,now->y,now->peso);
printf("\n+++\n\tDad (puntatore): %p\n\tson sinsitro (puntatore): %p\n\tson centrale (puntatore): %p\n\tson destro (puntatore): %p\n+++",now->dad,now->son_s,now->son_c,now->son_d);
}
struct elemento *aggiungi_figlio_s(int i, int j,struct elemento *now)
{
struct elemento *tmp;
if((tmp=(struct elemento *)malloc(sizeof(struct elemento)))==NULL)
{
printf("\nErrore creazione figlio da aggiungere");
return 0;
}
tmp->tipo='n';
tmp->x=i;
tmp->y=j;
tmp->peso=0.25;
tmp->dad=now;
tmp->son_s=NULL;
if(now->son_s==NULL)
{
now->son_s=tmp;
}
return tmp;
}
struct elemento *aggiungi_figlio_c(int i, int j,struct elemento *now)
{
struct elemento *tmp;
if((tmp=(struct elemento *)malloc(sizeof(struct elemento)))==NULL)
{
printf("\nErrore creazione figlio da aggiungere");
return 0;
}
tmp->tipo='n';
tmp->x=i;
tmp->y=j;
tmp->peso=0.5;
tmp->dad=now;
tmp->son_c=NULL;
if(now->son_c==NULL)
{
now->son_c=tmp;
}
return tmp;
}
struct elemento *aggiungi_figlio_d(int i, int j,struct elemento *now)
{
struct elemento *tmp;
if((tmp=(struct elemento *)malloc(sizeof(struct elemento)))==NULL)
{
printf("\nErrore creazione figlio da aggiungere");
return 0;
}
tmp->tipo='n';
tmp->x=i;
tmp->y=j;
tmp->peso=0.25;
tmp->dad=now;
tmp->son_d=NULL;
if(now->son_d==NULL)
{
now->son_d=tmp;
}
return tmp;
}
void trova_figlio(int i, int j,struct elemento *now)
{
int mat_v[3][5];
float arr_v[5];
if (Mat[i-1][j+1]==1) // Figlio sinistro
{
struct elemento *tmp1;
if((tmp1=(struct elemento *)malloc(sizeof(struct elemento)))==NULL)
{
printf("\nErrore creazione figlio da aggiungere");
}
printf("\nAggiunto sinistro");
tmp1=aggiungi_figlio_s(i-1,j+1,now);
printf("\nAggiunto");
printf("\n\tX: %d\n\tY: %d\n\tPeso: %f\n***",tmp1->x,tmp1->y,tmp1->peso);
print_node(tmp1);
printf("\nFiglio di");
printf("\n\tX: %d\n\tY: %d\n***",tmp1->dad->x,tmp1->dad->y);
getchar();
if((j<4)&&((i>-1)&&(i<4)))
{
trova_figlio(i-1,j+1,tmp1);
}
if(tmp1->y==4)
{
percorri_albero(tmp1,0,4,mat_v,arr_v);
}
}
if (Mat[i][j+1]==1) // Figlio centrale
{
struct elemento *tmp2;
if((tmp2=(struct elemento *)malloc(sizeof(struct elemento)))==NULL)
{
printf("\nErrore creazione figlio da aggiungere");
}
printf("\nAggiunto centrale");
tmp2=aggiungi_figlio_c(i,j+1,now);
printf("\nAggiunto");
printf("\n\tX: %d\n\tY: %d\n\tPeso: %f\n***",tmp2->x,tmp2->y,tmp2->peso);
print_node(tmp2);
printf("\nFiglio di");
printf("\n\tX: %d\n\tY: %d\n***",tmp2->dad->x,tmp2->dad->y);
getchar();
if (j<3)
{
trova_figlio(i,j+1,tmp2);
}
if(tmp2->y==4)
{
percorri_albero(tmp2,0,4,mat_v,arr_v);
}
}
if (Mat[i+1][j+1]==1) // Figlio destro
{
struct elemento *tmp3;
if((tmp3=(struct elemento *)malloc(sizeof(struct elemento)))==NULL)
{
printf("\nErrore creazione figlio da aggiungere");
}
printf("\nAggiunto destro");
tmp3=aggiungi_figlio_d(i+1,j+1,now);
printf("\nAggiunto");
printf("\n\tX: %d\n\tY: %d\n\tPeso: %f\n***",tmp3->x,tmp3->y,tmp3->peso);
print_node(tmp3);
printf("\nFiglio di");
printf("\n\tX: %d\n\tY: %d\n***",tmp3->dad->x,tmp3->dad->y);
getchar();
if((j<3)&&(i<4))
{
trova_figlio(i+1,j+1,tmp3);
}
if(tmp3->y==4)
{
percorri_albero(tmp3,0,4,mat_v,arr_v);
}
}
}
void percorri_albero(struct elemento *now, int Im, int v, int mat_v[3][5], float arr_v[5])
{
FILE *stream = fopen("alberi.txt","a");
int i=0;
int a=0;
int b=0;
int z=4;
float P;
i=Im;
z=v;
mat_v[1][i]=now->x;
fprintf(stream,"\nX:%d",mat_v[1][i]);
printf("\n\tX :%d",mat_v[1][i]);
mat_v[0][i]=now->y;
fprintf(stream,"\nY:%d",mat_v[0][i]);
printf("\n\tY :%d",mat_v[0][i]);
arr_v[i]=now->peso;
printf(stream,"\nPeso:%f",arr_v[z]);*/
i=i+1;
if (now->dad!=NULL)
{
printf("\nI:%d",i);
printf("\nZ:%d",z);
now=now->dad;
z--;
percorri_albero(now,i,z,mat_v,arr_v);
}
else
{
FILE *stream = fopen("alberi2.txt","a");
for(i=4;i>-1;i--)
{
fprintf(stream,"%d",mat_v[0][i]);
}
fprintf(stream,"\n");
for(i=4;i>-1;i--)
{
fprintf(stream,"%d",mat_v[1][i]);
}
fflush(stream);
fclose(stream);
P=arr_v[0];
stampa_prodotto(arr_v,P,1);
}
}
void stampa_prodotto(float arr_v[5], float P,int i)
{
FILE *stream = fopen("alberi2.txt","a");
if (i==5)
{
fprintf(stream,"\n");
fprintf(stream,"%f",P);
fflush(stream);
fclose(stream);
}
P=P*arr_v[i];
if((i==1)||(i==2)||(i==3)||(i==4))
{
i++;
stampa_prodotto(arr_v,P,i);
}
}
int main(char *argv, int argc)
{
int j=0;
int i=0;
for (i=0; i<5; i++)
{
printf("\nIndice i :%d --- Indice j :%d",i,j);
printf("\nMatrice :%d",Mat[i][0]);
getchar();
if(Mat[i][0]==1)
{
now=create_root(i,j);
printf("\nIndice i :%d --- Indice j :%d",i,j);
printf("\nCreata radice");
print_node(now);
getchar();
trova_figlio(i,j,now);
}
}
return 0;
}
-----------------------------------------------------
ERROR:
'main' : local function definitions are illegal
'stampa_prodotto' : local function definitions are illegal
'percorri_albero' : local function definitions are illegal
'trova_figlio' : local function definitions are illegal
'aggiungi_figlio_d' : local function definitions are illegal
'aggiungi_figlio_c' : local function definitions are illegal
'aggiungi_figlio_s' : local function definitions are illegal
'print_node' : local function definitions are illegal
'create_root' : local function definitions are illegal
|
|
|
|
|
Hi AndRy82,
i think you got the error because you insert your code at the wrong place.
This is the MyCLibrary.h i wrote for you.
You can compile it, but unfortunately you can't run it. The main problem is, you can't use every c-function in your c-code. The printf() is one of them, because where should go the output?
So I think you have to rewrite your c-code to c# code. It's not very complicated.
Good look,
Manfred
|
|
|
|
|
Hello ManiB
Firts, thanks for your help, I Make a mistake, I put printf in C code only to see if the program was working on correct data, in reality I need only to write into a file with fprintf. I have deleted all printf and I have make modify in the final part of the code:
public __gc class MyCLib
{
public:
//static int __gc Mat[5][5]={{0,0,0,0,0},{0,0,0,0,0},{1,1,1,1,1},{0,0,0,0,0},{0,0,0,0,0}}; //FAIL!
//static int Mat __gc [,] = new int __gc [5,5]; // ok
static public void test_main()
{
int i=0;
int j=0;
int Mat[5][5]={{0,0,0,0,0},{0,0,0,0,0},{1,1,1,1,1},{0,0,0,0,0},{0,0,0,0,0}};
for (i=0; i < 5; i++)
{
if(Mat[i][0]==1)
{
now=create_root(i,j);
trova_figlio(i,j,now,Mat);
}
}
}
};
}
if i run this there are't error and the application start, but it stop at this istructions -> now=create_root(i,j); says this problem: Additional information: Object reference not set to an instance of an object. I can't undersand.
I can bypass all of this problem only if in C language I can open a bitmap image and I can get the color of every pixel to make a matrix whit 1 if is black and 0 if is wihte, do you think that is possible?
thanks, Andrea.
|
|
|
|
|
|
First - this is truly EXCELLENT work. Second, there seems to be no way to read the current fore/background colors. Im wanting to implement an ANSI interpreted display (ESC[7m = reverse video, etc). Cursor movement and color works great, but in order to do reverse video, I need to be able to read the current colors.
Thanks again for this awesome work.
|
|
|
|
|
Hello xlar54,
thanks for using my library.
To get the current attribute of a charcter you need the function ReadConsoleOutputAttribute() and to get the current character itselve you need the function ReadConsoleOutputCharacter().
Both are not implemented in my library. Can you add them yourselve, or should i do the work?
Ciao, Manfred
|
|
|
|
|
project (in 'ConsoleLibrary.cpp' file) and it running without falling.
That what make me to believe that it had something with project's properties.
Can you write what should i add (or change) in project properties to be able to allocate memory?
Yossi
Ayallon valley - Israel
|
|
|
|
|
Hi,
you are right. The first times in my ConsoleLibrary the malloc function didn't catch an exception.
But then i included <math.h> and after than the exception appear!
It's very strange and I couldn't found the reason.
Sorry
Manfred
|
|
|
|
|
I'll try to find a way of doing it. If I'll find, I'll send you whatever I'll figured out.
If you'll find a way for doing it, I'll be very happy if you'll send me explanation about what ever you fount.
Have a nice day
Yossi
Ayallon valley - Israel
|
|
|
|
|
Hi Manfred
I try to find a way of using allocation (malloc) in c files that used in c# project. right now i have no idea how I'm going to do it. I would like to hear your opinion about a problem with allocation.
do you think it just a problem with properties definitions or maybe it a problem caused because of using c# compiler and there is no chance to do it ?
have a nice day
Yossi
Ayallon valley - Israel
|
|
|
|
|
Hi,
i think there must be an solution! Try to find a hint in other usegroups or google for it.
Sorry that i can't help you any more
Ciao,
Manfred
|
|
|
|
|
Hello Manfred
I need to use an allocation in C file that used in C# project. I try to use "malloc" but it falling during program's running.
I'm getting a message:
An unhandled exception of type 'System.NullReferenceException' occurred in clibrary.dll
Additional information: Object reference not set to an instance of an object.
how can it be solve?
my c file looked as:
------------------------------
<br />
#include<stdio.h><br />
#include<stdlib.h><br />
#include<malloc.h><br />
<br />
void AllocMem()<br />
{<br />
int *r;<br />
<br />
r = (int*)malloc(sizeof(int) * 25);<br />
}<br />
Yossi
Ayallon valley - Israel
|
|
|
|
|
Hello Manfred
I found out that i can join a header file with C functions to project.
while i imlement the function in header file - it's all work! but, i want to use C files to implement the external functions that appears in header file.
In code you wrote, I would like to join following code and files:
<br />
<br />
#pragma once<br />
<br />
#include "testFile.h"<br />
<br />
using namespace System;<br />
<br />
<br />
<br />
namespace CLibrary<br />
{<br />
public __gc class CLib<br />
{<br />
public:<br />
static int SumTest(int a, int b)<br />
{<br />
int sum = calcSum(a , b);<br />
return sum;<br />
}<br />
<br />
static void Sleep(int dwMiliseconds)<br />
{<br />
::Sleep(dwMiliseconds);<br />
}<br />
};<br />
}
--------------------------------
<br />
<br />
extern int calcSum(int a, int b);<br />
--------------------------------
<br />
<br />
#include <stdio.h><br />
#include <math.h><br />
<br />
extern int calcSum(int a, int b)<br />
{<br />
return (a + b);<br />
}<br />
i try to do it but there is always an compiler error.
is there is a way of doing it ?
Yossi
Ayallon valley - Israel
|
|
|
|
|
Hi Yossi,
you have to made some changes in your header file testFile.h:
<br />
#if _MSC_VER > 1000<br />
#pragma once<br />
#endif<br />
<br />
#ifndef _TESTFILE<br />
#define _TESTFILE<br />
<br />
#if !defined(_WIN32)<br />
#error ERROR: Only Win32 target supported!<br />
#endif<br />
<br />
#ifdef __cplusplus<br />
extern "C" {<br />
#endif<br />
<br />
<br />
extern int calcSum(int a, int b);<br />
<br />
#ifdef __cplusplus<br />
}<br />
#endif<br />
<br />
#endif<br />
It's very important to add the extern "C" { }
Ciao,
Manfred
|
|
|
|
|
Hi Manfred
I try to use your code for file called "tryCfile.h" that implemented by "tryCfile.c" but there is error
-------------------------------
// tryCfile.h
<br />
#if _MSC_VER > 1000<br />
#pragma once<br />
#endif<br />
<br />
#ifndef _TRYCFILE<br />
#define _TRYCFILE<br />
<br />
#if !defined(_WIN32)<br />
#error ERROR: Only Win32 target supported!<br />
#endif<br />
<br />
#ifdef __cplusplus<br />
extern "C" {<br />
#endif<br />
<br />
<br />
extern void doNothing(int s, int e);<br />
<br />
#ifdef __cplusplus<br />
}<br />
#endif<br />
<br />
#endif<br />
-------------------------------
// tryCfile.c
<br />
#include <stdio.h><br />
#include <math.h><br />
<br />
#include "tryCfile.h"<br />
#include "stdafx.h"<br />
<br />
void doNothing(int s, int e)<br />
{<br />
int r;<br />
<br />
r = 8*9;<br />
}<br />
thr error is "\\CLibrary\CLibrary\tryCfile.c(6): fatal error C1853: 'Debug/CLibrary.pch' precompiled header file is from a previous version of the compiler, or the precompiled header is C++ and you are using it from C (or vice versa) ".
how can it become solve ?
Yossi
Ayallon valley - Israel
|
|
|
|
|
Dont use precompiled headers for your testFile.c!
To do that rightclick on testFile.c in the project-map explorer and choose settings. Under C/C++ Precompiles headers you can disable it.
Ciao,
Manfred
|
|
|
|
|
Hello Manfred
i can compile the c file ! but i found an a problem in using functions
from a libraries. for example, i cant use "pow" function from math.h
library.
my code is
---------------------------
// "tryCfile.h"
<br />
#if _MSC_VER > 1000<br />
#pragma once<br />
#endif<br />
<br />
#ifndef _TRYCFILE<br />
#define _TRYCFILE<br />
<br />
#if !defined(_WIN32)<br />
#error ERROR: Only Win2 target supported!<br />
#endif<br />
<br />
#ifdef __cplusplus<br />
extern "C" {<br />
#endif<br />
<br />
<br />
extern void doNothing(int s, int e);<br />
<br />
#ifdef __cplusplus<br />
}<br />
#endif<br />
<br />
#endif<br />
---------------------------
// "tryCfile.c"
<br />
#include<stdio.h><br />
#include<math.h><br />
<br />
#include "tryCfile.h"<br />
<br />
void doNothing(int s, int e)<br />
{<br />
int index = 0;<br />
double u = pow(s,2) + pow(e,2);<br />
}<br />
the first line in 'doNothing' method (int index = 0) is working well but the other one (double u = pow(s,2) + pow(e,2)) produce a linking errors:
unresolved external symbol _main referenced in function _mainCRTStartup
fatal error LNK1120: 1 unresolved externals
how can it be resolve?
Yossi
Ayallon valley - Israel
|
|
|
|
|
Hi,
are you shure you compiled your c-file as c++-code?
To do that rightclick on testFile.c in the project-map explorer and choose settings. Under C/C++ advanced you can choose C++ code compilation (/Tp).
Ciao,
Manfred
|
|
|
|
|
Hi
Thank u for advice !!
but it working only for 'pow' method and not for other methods as 'sqrt'.
adding following code to method:
<br />
double u;<br />
u = sqrt(5.6);<br />
will produce following error messages
error LNK2019: unresolved external symbol _main referenced in function _mainCRTStartup
fatal error LNK1120: 1 unresolved externals
is there is any principle point here that can help me understand what is wrong and what i have to do in such a situation?
Yossi
Ayallon valley - Israel
|
|
|
|
|
Hi,
sorry, there is no principle point to solve such problems.
But it must be a project-property!
I found out that a standard-project disabled systemintenal functions to optimize the c++ code!
You have to enabled it.
To do that rightclick on testFile.c in the project-map explorer and choose settings. Under C/C++ optimization you can choose activate systeminternal functions (/Oi).
Ciao,
Manfred
|
|
|
|
|