Solved

convert simple c++ code to equivilant c code?

Posted on 2004-10-07
6
655 Views
Last Modified: 2008-01-09
hi,

i am a .net developer and know a small bit about c/c++

i dont know if this is possible, but i have a program written in c as a DLL that NEARLY does what i need.  im basically trying to get it to pass one of its strings back to .net code by supplying .net with a function that it can call to get the string in question

i have made up a test C++ DLL with the same type of code that i need and it passes the string back perfectly to .net.  but when i try to use this code in the c DLL, it fails.  i think it is because i dont have the stdafx.h included - and i cant for the life of me get it to compile when i do include it.  is there another way around this? or does anyone know how to get these two functions to work in C without including that file?

the only restriction is that they must be exactly like below in terms of the function argument - i.e. i need to pass in a char**  and i'm nearly sure i need to release it as per the other function to avoid memory leaks.  i also need the shared data segment - since this is what is used in the other c DLL

thanks!

// Shared DATA declaration
#pragma data_seg(".SHARDATA")
#define STRING_SIZE 255
char acString[STRING_SIZE] = "\0";
#pragma data_seg()

__declspec( dllexport ) void getString(char** p)
{
    *p = new char[STRING_SIZE];
      strncpy ( acString, "test string", STRING_SIZE - 1);
      strcpy(*p, acString);
}

__declspec( dllexport) void ReleaseString(char* p)
{
    delete[] p;
}
0
Comment
Question by:sdlangers
  • 4
  • 2
6 Comments
 
LVL 12

Accepted Solution

by:
stefan73 earned 125 total points
ID: 12246851
Hi sdlangers,
*p = new char[STRING_SIZE];
*p = (char*)malloc(STRING_SIZE);

delete[] p;
free(f);

The rest is C already.

Cheers!

Stefan
0
 

Author Comment

by:sdlangers
ID: 12250991
stefan,

*p = new char[STRING_SIZE];

doesnt compile - it complains about the new keyword
0
 

Author Comment

by:sdlangers
ID: 12251013
oh im sorry-  i think i see what you're saying - i should switch those lines for the ones below in your answer

ok - let me try that

thanks!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:sdlangers
ID: 12251678
thank you - worked perfectly
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12257660
Fine. Just be careful: malloc takes the size in bytes. The difference doesn't matter with chars, but with bigger data types:

long* l = new long[LONG_SIZE];

must be replaced with

long* l = (long*)malloc(LONG_SIZE*sizeof(long));

...so you could also write

*p = (char*)malloc(STRING_SIZE*sizeof(char));
0
 

Author Comment

by:sdlangers
ID: 12260743
will do.  thank you again.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now