Solved

convert simple c++ code to equivilant c code?

Posted on 2004-10-07
6
656 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 104
C++ Language error 28 186
convert char array to number in c 5 83
C++ :Change value from  DisableCMD registry 4 44
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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

929 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

7 Experts available now in Live!

Get 1:1 Help Now