Export a 'long' from unmanaged code into .net
-
Hi, I have to export a 'long' value from an unmanaged dll to a VB app. I do this with other data types like int and bool, works fine, so I more or less know how do do it. When I do the same with 'long' the call crashes. Where there any samples out? What is so special with 'long'? Thanks for helping. With best regards Gerhard
extern "C" { __declspec(dllexport) long GetCurrentFileRawFilePointer (void); }
extern "C" { __declspec(dllexport) int AwaitCycleDoneFor (uint); }
extern "C" { __declspec(dllexport) bool isDataLinkClear (void); }First line fails, other worked fine.
Declare Function GetCurrentFileRawFilePointer Lib "GeoMon4D_iMX7.dll" () As Long
Declare Function AwaitCycleDoneFor Lib "GeoMon4D_iMX7.dll" (timeout_ms As UInteger) As Integer
Declare Function isDataLinkClear Lib "GeoMon4D_iMX7.dll" () As Boolean -
Hi, I have to export a 'long' value from an unmanaged dll to a VB app. I do this with other data types like int and bool, works fine, so I more or less know how do do it. When I do the same with 'long' the call crashes. Where there any samples out? What is so special with 'long'? Thanks for helping. With best regards Gerhard
extern "C" { __declspec(dllexport) long GetCurrentFileRawFilePointer (void); }
extern "C" { __declspec(dllexport) int AwaitCycleDoneFor (uint); }
extern "C" { __declspec(dllexport) bool isDataLinkClear (void); }First line fails, other worked fine.
Declare Function GetCurrentFileRawFilePointer Lib "GeoMon4D_iMX7.dll" () As Long
Declare Function AwaitCycleDoneFor Lib "GeoMon4D_iMX7.dll" (timeout_ms As UInteger) As Integer
Declare Function isDataLinkClear Lib "GeoMon4D_iMX7.dll" () As BooleanGerhardKreuzer wrote:
extern "C" { __declspec(dllexport) bool isDataLinkClear (void); }
It's because of the extra spaces in this line.
Jeremy Falcon
-
Hi, I have to export a 'long' value from an unmanaged dll to a VB app. I do this with other data types like int and bool, works fine, so I more or less know how do do it. When I do the same with 'long' the call crashes. Where there any samples out? What is so special with 'long'? Thanks for helping. With best regards Gerhard
extern "C" { __declspec(dllexport) long GetCurrentFileRawFilePointer (void); }
extern "C" { __declspec(dllexport) int AwaitCycleDoneFor (uint); }
extern "C" { __declspec(dllexport) bool isDataLinkClear (void); }First line fails, other worked fine.
Declare Function GetCurrentFileRawFilePointer Lib "GeoMon4D_iMX7.dll" () As Long
Declare Function AwaitCycleDoneFor Lib "GeoMon4D_iMX7.dll" (timeout_ms As UInteger) As Integer
Declare Function isDataLinkClear Lib "GeoMon4D_iMX7.dll" () As BooleanIt's a long value, but the VB code requires a short - so you will need to zip the value - which should compress it nicely - and then pass it.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
Hi, I have to export a 'long' value from an unmanaged dll to a VB app. I do this with other data types like int and bool, works fine, so I more or less know how do do it. When I do the same with 'long' the call crashes. Where there any samples out? What is so special with 'long'? Thanks for helping. With best regards Gerhard
extern "C" { __declspec(dllexport) long GetCurrentFileRawFilePointer (void); }
extern "C" { __declspec(dllexport) int AwaitCycleDoneFor (uint); }
extern "C" { __declspec(dllexport) bool isDataLinkClear (void); }First line fails, other worked fine.
Declare Function GetCurrentFileRawFilePointer Lib "GeoMon4D_iMX7.dll" () As Long
Declare Function AwaitCycleDoneFor Lib "GeoMon4D_iMX7.dll" (timeout_ms As UInteger) As Integer
Declare Function isDataLinkClear Lib "GeoMon4D_iMX7.dll" () As BooleanI used C/C++ many years ago, as I recall that the
long
in C/C++ is actually 32 bits, the 64 bits type is calledlong long
(or something like it), whereaslong
in .NET (and many other later languages) is 64 bits (see [here](<code>long</code>)[[^](<code>long</code>)]). -
Hi, I have to export a 'long' value from an unmanaged dll to a VB app. I do this with other data types like int and bool, works fine, so I more or less know how do do it. When I do the same with 'long' the call crashes. Where there any samples out? What is so special with 'long'? Thanks for helping. With best regards Gerhard
extern "C" { __declspec(dllexport) long GetCurrentFileRawFilePointer (void); }
extern "C" { __declspec(dllexport) int AwaitCycleDoneFor (uint); }
extern "C" { __declspec(dllexport) bool isDataLinkClear (void); }First line fails, other worked fine.
Declare Function GetCurrentFileRawFilePointer Lib "GeoMon4D_iMX7.dll" () As Long
Declare Function AwaitCycleDoneFor Lib "GeoMon4D_iMX7.dll" (timeout_ms As UInteger) As Integer
Declare Function isDataLinkClear Lib "GeoMon4D_iMX7.dll" () As BooleanIf none of the other approaches work, consider a struct. struct MyLong { int hi; int lo; } In the C/C++ side you might be able to union with a long, but it is probably more platform portable to explicitly crack the long and assign the two members separately.
-
It's a long value, but the VB code requires a short - so you will need to zip the value - which should compress it nicely - and then pass it.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
Better to remove the zero-bits because they are worthless anyway.
-
Hi, I have to export a 'long' value from an unmanaged dll to a VB app. I do this with other data types like int and bool, works fine, so I more or less know how do do it. When I do the same with 'long' the call crashes. Where there any samples out? What is so special with 'long'? Thanks for helping. With best regards Gerhard
extern "C" { __declspec(dllexport) long GetCurrentFileRawFilePointer (void); }
extern "C" { __declspec(dllexport) int AwaitCycleDoneFor (uint); }
extern "C" { __declspec(dllexport) bool isDataLinkClear (void); }First line fails, other worked fine.
Declare Function GetCurrentFileRawFilePointer Lib "GeoMon4D_iMX7.dll" () As Long
Declare Function AwaitCycleDoneFor Lib "GeoMon4D_iMX7.dll" (timeout_ms As UInteger) As Integer
Declare Function isDataLinkClear Lib "GeoMon4D_iMX7.dll" () As BooleanYour long is actually a pointer. A pointer is unsigned 4 bytes or 8 bytes on 32-bit and 64-bit platform respectively. Long is always signed 64-bit integer on .NET. And you are not supposed to use a native file pointer in .NET to read/write to a file. Restrict your file operations to native code.
extern "C" { __declspec(dllexport) void* GetCurrentFileRawFilePointer (void); }
Declare Function GetCurrentFileRawFilePointer Lib "GeoMon4D_iMX7.dll" () As IntPtr
-
I used C/C++ many years ago, as I recall that the
long
in C/C++ is actually 32 bits, the 64 bits type is calledlong long
(or something like it), whereaslong
in .NET (and many other later languages) is 64 bits (see [here](<code>long</code>)[[^](<code>long</code>)]).Many years ago, true (mostly). Today, at least on the 64 bit Archs (x86-64, arm-64, risc-v-64) I'm aware of, a long is 64 bits, as is a long long. And going back even further in time (8086 days), an int was 16 bits, and longs and long longs were 32 bits.
Keep Calm and Carry On
-
Many years ago, true (mostly). Today, at least on the 64 bit Archs (x86-64, arm-64, risc-v-64) I'm aware of, a long is 64 bits, as is a long long. And going back even further in time (8086 days), an int was 16 bits, and longs and long longs were 32 bits.
Keep Calm and Carry On
so they are referring to kind of physical unit rather than logical unit ...
-
If none of the other approaches work, consider a struct. struct MyLong { int hi; int lo; } In the C/C++ side you might be able to union with a long, but it is probably more platform portable to explicitly crack the long and assign the two members separately.
-
This is the way to do it. You could cast it, but it's never going to be as easy, safe and predictable as using a struct.
This is the way. :RemovesHelmet:
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
Many years ago, true (mostly). Today, at least on the 64 bit Archs (x86-64, arm-64, risc-v-64) I'm aware of, a long is 64 bits, as is a long long. And going back even further in time (8086 days), an int was 16 bits, and longs and long longs were 32 bits.
Keep Calm and Carry On
-
This is the way. :RemovesHelmet:
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
My other option was struct MyLong { int hi; uint lo; } You only need the sign in one spot. Am I right? :Reapplies Helmet: ? :Puts Helmet on head and fastens chin strap and lowers visor: ? What is the opposite of “Removes Helmet” that is just as concise?
-
My other option was struct MyLong { int hi; uint lo; } You only need the sign in one spot. Am I right? :Reapplies Helmet: ? :Puts Helmet on head and fastens chin strap and lowers visor: ? What is the opposite of “Removes Helmet” that is just as concise?
englebart wrote:
What is the opposite of “Removes Helmet” that is just as concise?
"Equips helmet"?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
englebart wrote:
What is the opposite of “Removes Helmet” that is just as concise?
"Equips helmet"?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
Hi, I have to export a 'long' value from an unmanaged dll to a VB app. I do this with other data types like int and bool, works fine, so I more or less know how do do it. When I do the same with 'long' the call crashes. Where there any samples out? What is so special with 'long'? Thanks for helping. With best regards Gerhard
extern "C" { __declspec(dllexport) long GetCurrentFileRawFilePointer (void); }
extern "C" { __declspec(dllexport) int AwaitCycleDoneFor (uint); }
extern "C" { __declspec(dllexport) bool isDataLinkClear (void); }First line fails, other worked fine.
Declare Function GetCurrentFileRawFilePointer Lib "GeoMon4D_iMX7.dll" () As Long
Declare Function AwaitCycleDoneFor Lib "GeoMon4D_iMX7.dll" (timeout_ms As UInteger) As Integer
Declare Function isDataLinkClear Lib "GeoMon4D_iMX7.dll" () As BooleanHi, lastly I found a working solution and its far from zipping data, thanks for that really good idea, but it was not April 1st, (and gives insight into your mindset). The embedded framework didn't support passing 64 bit values between managed and unmanaged code. A little workaround is needed. Fist create a new IntPnt object and pass this object to unmanged code by reference as parameter of some function. In unmanaged code use this pointer as any other pointer in C/C++, and assign the data you need. Return from this unmanaged function and use the 'Marshal' class to readout the value. Use a 'try finally' block to clean up memory in any case. Managed code:
Private Shared Function ReadFilePointer() As Long
Dim ptr As IntPtr = Marshal.AllocHGlobal(Len(New Long()))
Try
GetBookmark(ptr)
Return Marshal.ReadInt64(ptr)
Finally
Marshal.FreeHGlobal(ptr)
End Try
End FunctionUnmanaged code:
extern "C" { __declspec(dllexport) void GetBookmark (intptr_t*); }
void GetBookmark(intptr_t* bmark)
{
*bmark = ftell(outFile);
}Hope this can help someone someday. With best regards Gerhard
-
My other option was struct MyLong { int hi; uint lo; } You only need the sign in one spot. Am I right? :Reapplies Helmet: ? :Puts Helmet on head and fastens chin strap and lowers visor: ? What is the opposite of “Removes Helmet” that is just as concise?