473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How to define return string in DLL?

Hi All,

I need to make DLL for my new hash function in C. In myhashdll.c file,
I have a function as:

char *make_hash(char *in, char *out)
{
......
......
return (out);
}

So, this function is supposed to return a hashed string in the *out
argument. What I don't know is how to define its header file. Say, in
myhashdll.h file, I need to define 2 values that tell my main() whether
this make_hash() call works good or not. If do defines as:

#define HASH_OK 0
#define HASH_ERR 44

I think these 2 defines should not work right, because my return will
be a hashed sring, instead of 2 numbers. Can anybody help me to figure
out how to do these 2 defines? I mean, to make the defines that can
return my hashed string.

Sorry for the complexity of the question. Thanks in advance!

Huey

Nov 14 '05 #1
2 2573
One thing. Why do you return a char* if 0 is going to be ok? If I was
going to write such a function I would make a couple of changes. First
of all I would make the definition something like this:

int make_hash(char* in, size_t insize, char* out, size_t outsize);

This makes it possible to make a hash of something that's not \0
terminated. I would also alow me to return different errorcodes for
different errors. The outsize I would use as a maximum size for the
output. This make the caller responsible for allocating the memory and
I usaly find that more safe for memory leaks. Another thing about the
outsize is that with a hash youknow the size. It will always be the
same and therefor you are able to hash define the size.

So basicly, theres seems to be no good reason for you to return a
char*, and second of all, why return NULL on success?

--
bjrnove

Nov 14 '05 #2
bjrnove wrote:

One thing. Why do you return a char* if 0 is going to be ok? If I was
going to write such a function I would make a couple of changes. First
of all I would make the definition something like this:

int make_hash(char* in, size_t insize, char* out, size_t outsize);

This makes it possible to make a hash of something that's not \0
terminated. I would also alow me to return different errorcodes for
different errors. The outsize I would use as a maximum size for the
output. This make the caller responsible for allocating the memory and
I usaly find that more safe for memory leaks. Another thing about the
outsize is that with a hash youknow the size. It will always be the
same and therefor you are able to hash define the size.


Keep things simple. The point of a hash is to return an integer of
some size, nothing more (apart from the distribution). So there is
no need to allocate any memory anywhere. The most common use of a
hash is on nul terminated strings, so supplying a length parameter
means prescanning the string with strlen, and will be a significant
unneeded slowdown. What errors (apart from hardware) can occur? A
properly designed hash will have no integer overflows, etc. so an
error return is pointless. Even if I am wrong, some hash
algorithms cannot return zero (such as a CRC with proper
initialization) so a zero error return is perfectly feasible.

You can find some hash functions and tests on them in:

<http://cbfalconer.home.att/download/hshftst.zip>

which you can easily modify to include further functions. It uses
the instrumentation in hashlib to measure the efficacy of the hash
on the data.

DLLs don't enter into it. They are meaningless to the C language,
and thus OT here.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: QQ | last post by:
How to define a string? Usually we can #define MAX 30 However if I'd like to define a string const can I? #define S "Hello"
1
by: Daylor | last post by:
how can i return string to .Net application from c dll ? i have the text in char sText ; and i want to return it from c dll to .Net application. i tried : *char in the function, but the...
10
by: muntyanu | last post by:
Hi all, In C# system service I need to get some string from MFC mainframe window by SendMessage() API call. I managed to send message, but can not find the way to return string to C# app. Is...
1
by: Scanner2001 | last post by:
I am looking for a way to return the output of a web service as a string without the xml tags, just the raw data. I am calling the web service from an html page using a form post method....
18
by: Ed Jay | last post by:
<disclaimer>js newbie</disclaimer> My page has a form comprised of several radio buttons. I want to poll the buttons to determine which button was selected and convert its value to a string. I...
22
by: Michael Rasmussen | last post by:
Hi all, Not sure if this is OT? I have a function in a library written in C++ which returns a strdup(s.c_str()) to an application written i C. Running Valgrind on my C-application shows this...
17
by: streamkid | last post by:
Why can't a function not return string? (error: new types may not be defined in a return type)... How can i solve it? I thought of passing the string into the function, but that will make me...
3
by: Jack | last post by:
#include <string> #include <iostream> int main(){ string s= "Application"; return 0; } What am i doing wrong, I get an error message saying 'string undeclared
13
by: Andreas Eibach | last post by:
Hi, let's say I have this: #include <string.h> #define BLAH "foo" Later on, I do this:
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.