https://github.com/synergex/sampledll
This code sample demonstrates three different use cases for returning values from a DLL routine: 1. Returning a simple integer value 2. Returning a pointer to a data structure 3. Returning a pointer to a pointer to a data structure
https://github.com/synergex/sampledll
Last synced: 12 days ago
JSON representation
This code sample demonstrates three different use cases for returning values from a DLL routine: 1. Returning a simple integer value 2. Returning a pointer to a data structure 3. Returning a pointer to a pointer to a data structure
- Host: GitHub
- URL: https://github.com/synergex/sampledll
- Owner: Synergex
- License: other
- Created: 2021-12-16T17:16:01.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-07-25T20:12:12.000Z (almost 4 years ago)
- Last Synced: 2025-11-09T12:02:27.412Z (8 months ago)
- Language: C
- Homepage:
- Size: 18.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# SampleDLL
**Created Date:** 11/17/2009
**Last Updated:** 11/17/2009
**Description:** This code sample demonstrates three different use cases for returning values from a DLL routine: 1. Returning a simple integer value 2. Returning a pointer to a data structure 3. Returning a pointer to a pointer to a data structure
**Platforms:** Windows
**Products:** Synergy DBL; DLL API
**Minimum Version:** 9.1.5
**Author:** Chip Camden
**Additional Information:**
File included:
sample.dbl Example Synergy/DE program that calls the DLL routines
and processes the returned values.
SampleDLL.sln A Microsoft Visual Studio solution for building the DLL
SampleDLL/*.* Source files for the DLL (see especially SampleDll.(cpp,h))
Release/SampleDLL.dll A 32-bit version of the DLL
x64/Release/SampleDLL.dll A 64-bit version of the DLL
NOTES ON SPECIFIC FUNCTIONS
fnSampleDLL
This function merely returns an integer value.
fnSampleDLLptr
This function returns a pointer to a data structure SAMPLESTRUCT
that is defined in SampleDLL.h and duplicated as sampleStruct in
sample.dll. Note that the C++ routine returns the address of
one of these structures, which MUST BE PERSISTENT. In this case,
we use a static. If the structure were an auto variable, then it
would be released upon return from this routine, which could cause
unpredictable failures.
In the Synergy/DE code, the pointer is dereferenced by registering
a memory handle to the address, and mapping it to the structure
that we have defined to match the structure from the DLL.
fnSampleDLLptrptr
This function returns a pointer to a pointer to the same data
structure. In this case, we must first dereference the returned
pointer to obtain a pointer to the structure. The same rules
apply with regard to the persistence of the items referenced --
in this case, both the structure and the pointer to it must
persist beyond the return from the routine, so statics are used
for both.
On the Synergy/DE side, we register a handle to the returned address,
then treat that handle as referencing an integer value of the size
D_ADDRSIZE (the size of a pointer). This will work for both 32-bit
and 64-bit platforms, because we use D_ADDRSIZE. Then we can take
that value and treat it as a pointer, the same as if it had been
returned by fnSampleDLLptr.
The same principle can be applied to pointers that are returned as
members of a data structure, or a pointer to an array of pointers.
In the latter case, simply increment the pointer to the array by
D_ADDRSIZE to access the next pointer in the array.