Click here to Skip to main content
16,016,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ALL,

I 'm currently having problems dealing with passing a 'time_t' variable back and forth between a C and a C# apps.
My two apps communicate back and forth quite well with passing char and int variables. I have already built a Windows
DLL file with a lot of C functions, which the C# App calls. The communications between the C# APP and the windows dll
Api's are going quite well.

1) I want to pass back to C# from a DLL "C" routine, having a referenced time_t parameter holding a computed time
value. A current C# function call looks like this,

C#
[DllImport("mylibrary.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int GETEPOCHNUMBERFROMDATE(StringBuilder inpdate, ref long lreturnnumber, ref int iostatx);


The receiving "C" function declaration is as follows:
C<b></b>
GETEPOCHNUMBERFROMDATE(char * datevar,  time_t *  time_tvalue,  int * errstatval) {


I have to replace the C#'s call's 2nd parameter to "what" to match the "C" functions 2nd parameter; so that they
are the same data type, size, and etc; so that they can pass back and forth data seemlessly and flawlessly.
----------------
2) Using the same Function Declaration in the above mentioned call for a different problem. The 2nd parameter of the sending "C"
parameter declaration, is given a return value of -2, and the C# calling function, returns with the 2nd parameter having a 9 digit number.
It's not the -2 that it was given to be sent back. What am I doing wrong?

I'm looking for some solid, visible examples, for both these two problems, so I can see what I've done wrong, so I can mend my
ways and so I can get my two apps working properly again. So what other pieces of information have I missed, that is needed.

Thanks again for any help, in solving these two problem.
TT, May 26, 2023.
This is not a school(of any kind) assignment.

What I have tried:

What have I tried?? C/C++ and C# Code examples, MS Documentation, and from past work experiences. And have searched the internet for answers to questions. Used google looking for answers.
Posted
Updated 26-May-23 23:43pm
v2
Comments
Shao Voon Wong 28-May-23 5:14am    
Is the 9-digit 4294967294 (0xFFFFFFFE)? You should post what is the 9-digit you are getting.

time_t is a 32 bit integer, not a 64 bit long - so the returned parameters as a long value will contain both the time_t and the int values.
This probably will fix both of your problems.
 
Share this answer
 
Comments
Richard MacCutchan 27-May-23 5:37am    
Actually time_t is now a 64bit, unless you use the option to select a 32 bit type. time, _time32, _time64 | Microsoft Learn[^].
samtoad 2-Jun-23 11:03am    
You know,... with a little kindness, courtesy, and consideration are attributes; when working with people, can go a long way if used correctly. I have the answer(s) that I was looking for, and with a little bit of rewriting; it works! The answer(s) turned about to be very simple and could have even
amazed you. Thanks.
See time, _time32, _time64 | Microsoft Learn[^] for a description of the value ranges for the time_t type. You just need to make sure the C code is not selecting the 32bit type.

Also, I do not think you can use a StringBuilder reference in C# to correcpond to a char* in C; but I will check it out to see.

[edit]
I have created a test version of your problem and it works fine. The dll can read the data from the StringBuilder object. Also, the returned time value is correctly stored as a long integer.

So if your code is not working then you need to update your question (use the Improve question link above) with complete details of the code and what is not working.)

[/edit]
 
Share this answer
 
v4
Comments
samtoad 27-May-23 23:07pm    
Thanks OriginalGriff and Richard MacCutchan. But in the mean time, what about providing some solid/working examples as requested, showing me what I have done wrong with some explanation, and then show me your working examples of how you fixed the problems. Like the following statement: "I have created a test version of your problem and it works fine. The dll can read the data from the StringBuilder object. Also, the returned time value is correctly stored as a long integer."

As for this statement, ""Also, I do not think you can use a StringBuilder reference in C# to corresponde to a char* in C;"", I have been using StringBuilder reference in C# to corresponde to a char * charvariable for a long time and they have worked quite well, and I have had no problems with them -- as you mentioned to the contrary.

I have worked on this project for a long time, and have worked out a lot of problems on my own. But now, I'm only looking for some help. Not a confrontation. Please, can you help.
Richard MacCutchan 28-May-23 2:47am    
"... showing me what I have done wrong"
Well, you have not shown us your code, nor have you explained what the problem is. I just took your two function definitions and put some code in them to test them out, and as I said it works fine. So you need to show us your code and explain what is wrong with the results you get.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900