Solved

convert simple c++ code to equivilant c code?

Posted on 2004-10-07
6
660 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
Independent Software Vendors: 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

680 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