Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

convert simple c++ code to equivilant c code?

Posted on 2004-10-07
6
Medium Priority
?
669 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 500 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.
Suggested Courses

772 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