Solved

convert simple c++ code to equivilant c code?

Posted on 2004-10-07
6
663 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

617 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