I have a C header file (it's a part of some SDK) and there is a typedef which depends on system architecture (whether it is 32 or 64-bit), how do I transfer it to my D module? Thanks.
Edit: OK, that was too simple and I've already find a solution...
If someone interested, it is:
version(X86) {
typedef int your_type;
}
version(X86_64) {
typedef long your_type;
}
version(X86)
{
// 32-bit
}
else
version(X86_64)
{
// 64-bit
}
else
{
// none of the above
}
Source: http://digitalmars.com/d/2.0/version.html
Related
I'm building an app for Android and iOS and I want to reuse as much code as possible. I have some generic C code (an algorithm) that doesn't include any system library. Is it possible to expose it to my common Kotlin source set using cinterop or any other tool?
My build.gradle.kts:
plugins {
id("com.android.library")
kotlin("multiplatform")
kotlin("native.cocoapods")
}
android {
compileSdkVersion(29)
defaultConfig {
minSdkVersion(21)
targetSdkVersion(29)
}
sourceSets.all {
manifest.srcFile("src/androidMain/AndroidManifest.xml")
java.srcDirs("src/androidMain/java")
res.srcDirs("src/androidMain/res")
}
}
version = "1.0"
kotlin {
android()
ios()
cocoapods {
// Configure fields required by CocoaPods.
summary = "..."
homepage = "..."
// You can change the name of the produced framework.
// By default, it is the name of the Gradle project.
frameworkName = "SharedModule"
}
// Workaround for ios platform imports to work on Android Studio
// iosX64("ios")
sourceSets["commonMain"].dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
}
sourceSets["androidMain"].dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib")
}
sourceSets.all {
languageSettings.progressiveMode = true
}
}
Not automatically. On the JVM side you'll need to use JNI to talk to the C code, and on the native side you'll need to use Kotlin cinterop. JNI and Kotlin cinterop will have an interface to the C code that's very similar to each other, but not the same. To expose that to common code, you'll need to write a common API layer that delegates to JNI code on the JVM and Kotlin cinterop code on native.
Wrapping very similar platform-specific API's is pretty straightforward once you get used to it. Ideally, you could automatically wrap them, but right now you can't. I gave a talk that discusses some techniques for this: https://vimeo.com/371460823
Below follows a simple example of how to call c code within Kotlin:
cCaller.kt
class cCaller {
init {
System.loadLibrary("cCode")
} external fun callCFunction()
}
In your code:
fun main() {
cCaller().callCFunction()
}
cCode.c
#include <stdio.h>
#include "cCaller.h"JNIEXPORT void JNICALL Java_cCaller_callCFunction(JNIEnv *env, jobject obj) {
// YOUR CODE HERE
return;
}
Notice that the function callCFunction is prefixed with Java_ and cCaller_
Also notice the #include "cCaller.h" added to the second line of cCode.c - we need to create this file:
cCaller.h
#include <jni.h>#ifndef _Included_NativeSample
#define _Included_NativeSample
#ifdef __cplusplus
extern "C" {
#endifJNIEXPORT void JNICALL Java_cCaller_callCFunction(JNIEnv *, jobject);#ifdef __cplusplus
}
#endif
#endif
Compile
gcc cCode.c -o libcCode.so -shared -fPIC -I <jdk_path>/include -I <jdk_path>/include/linux
I'm trying to learn Swift by converting an existing Objective-C app I wrote (a streaming audio player). However, the libraries I'm using don't have Swift equivalents, so I've created a bridging header and am referencing them successfully.
Now, I'm trying to do a switch on an enum from one of these libraries, but Swift is just complaining.
To make things even more confusing (for me, probably not for any of you), a series of if blocks works fine.
Here is the enum from the Objective-C library:
typedef enum {
kFsAudioStreamRetrievingURL,
kFsAudioStreamStopped,
kFsAudioStreamBuffering,
kFsAudioStreamPlaying,
kFsAudioStreamPaused,
kFsAudioStreamSeeking,
kFSAudioStreamEndOfFile,
kFsAudioStreamFailed,
kFsAudioStreamRetryingStarted,
kFsAudioStreamRetryingSucceeded,
kFsAudioStreamRetryingFailed,
kFsAudioStreamPlaybackCompleted,
kFsAudioStreamUnknownState
} FSAudioStreamState;
This works:
if (state == kFsAudioStreamRetrievingURL) {
}
if (state == kFsAudioStreamStopped) {
}
if (state == kFsAudioStreamBuffering) {
}
But this doesn't work:
switch state {
case kFsAudioStreamRetrievingURL:
case kFsAudioStreamStopped:
case kFsAudioStreamBuffering:
break;
}
The second one gives me the following error:
Binary operator '~=' cannot be applied to two 'FSAudioStreamState' operands
I would appreciate any assistance in helping me learn more Swift!
Try this:
switch state {
case .RetrievingURL:
case .Stopped:
case .Buffering:
...
}
---- EDITED!!! ----
I'm wrong, this is C++ enum. So try this:
switch state.value {
case kFsAudioStreamRetrievingURL.value:
case kFsAudioStreamStopped.value:
case kFsAudioStreamBuffering.value:
...
}
In obj-c
Enum definition should be
typedef NS_ENUM(NSInteger, FSAudioStreamState){
FSAudioStreamStateStopped,
...
}
So when they are imported into Swift
enum FSAudioStreamState{
case Stopped
...
}
I think it's C++ enum (Not Objective-C enum), so swift will not regard it as enum and it would be better to use if - else.
I have created a GUI using tcl. I want to make some of the core functionalities of the tcl code available to be used by any program which supports dll. For that i have taken a very simple tcl code example, which adds two integer numbers and i have written a c wrapper function to use this functionality. This is working for me. Now how can i create a dll for these two c and tcl files, so that any program can use this addition functionality by simply loading the dll.
Here is my simple tcl code :
/* Filename : simple_addition.tcl */
#!/usr/bin/env tclsh8.5
proc add_two_nos { } {
set a 10
set b 20
set c [expr { $a + $b } ]
puts " c is $c ......."
}
And here is my c wrapper function which uses the above tcl addition functionality :
#include <tcl.h>
#include <tclDecls.h>
#include <tclPlatDecls.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char **argv) {
Tcl_Interp *interp;
int code;
char *result;
printf("inside main function \n");
Tcl_FindExecutable(argv[0]);
interp = Tcl_CreateInterp();
code = Tcl_Eval(interp, "source simple_addition.tcl; add_two_nos");
/* Retrieve the result... */
result = Tcl_GetString(Tcl_GetObjResult(interp));
/* Check for error! If an error, message is result. */
if (code == TCL_ERROR) {
fprintf(stderr, "ERROR in script: %s\n", result);
exit(1);
}
/* Print (normal) result if non-empty; we'll skip handling encodings for now */
if (strlen(result)) {
printf("%s\n", result);
}
/* Clean up */
Tcl_DeleteInterp(interp);
exit(0);
}
This c wrapper is working fine for me and gives correct results.
Now I want to create a dll file, so that if i include that dll to any program that supports dll, it should be able to use this addition functionality of the above tcl code. Can anybody please tell me the way i can do it. Please help me. I am new to this dll concept.
In order to create the .dll you'll have to use something like Visual Studio and C or C++ to create the .dll (there are lots of other tools out there that can create .dll files but VS is easy to get hold of and to use.) So in VS create a new project, this needs to be a C++ WIN32 project. Select the DLL application type and the Export Symbols additional option.
VS will create a basic .dll that you can then amend to do what you want. I short I'd look at putting the creating/destruction of the intrepter into the dllmain:
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
Tcl_FindExecutable(NULL);
interp = Tcl_CreateInterp();
}
case DLL_THREAD_ATTACH:
break ;
case DLL_THREAD_DETACH:
break ;
case DLL_PROCESS_DETACH:
{
Tcl_DeleteInterp(interp);
break;
}
}
return TRUE;
}
and then create functions exported by the .dll that make use of the interpreter. If you aren't familiar with the concept of shared libaries then I'd suggest spending a little time reading up on them, try here and here for some background reading.
i have got a ScreenCameraSDK and it comes with a 11kb dll file, it has a documentation too which lists the functions which can be used. It says
ScreenCamera SDK ActiveX Reference Documentation
ActiveX Reference
The ActiveX ID on the system is: ScreenCameraSDK.RemoteControl
Every method on the interface returns FAIL or SUCCESS. (0 or 1).
Create an instance of the ActiveX on your application, and then call InitializeScreenCameraRemoteControl. If the return value is SUCCESS then ScreenCamera is properly installed and you can then call any other method on the ActiveX's interface. If not ScreenCamera could not be found and you should contact support.**
Now my question is, i have the dll and no other files. How can i use the functions inside it in a VC++ Project with Visual Studio 2008.
Thanks
I TRIED THE FOLLOWING CODE BUT GOT COMPILATION ERROR OF UNDEFINED IDENTIFIER
#include <stdio.h>
// This is the path for your DLL.
// Make sure that you specify the exact path.
#import "e:\ScreenCameraSDK.dll" no_namespace
void main()
{
BSTR bstrDesc;
try
{
CoInitialize(NULL);
short st = 2;
short st1;
// Declare the Interface Pointer for your Visual Basic object. Here,
// _Class1Ptr is the Smart pointer wrapper class representing the
// default interface of the Visual Basic object.
_Class1Ptr ptr;
// Create an instance of your Visual Basic object, here
// __uuidof(Class1) gets the CLSID of your Visual Basic object.
ptr.CreateInstance(__uuidof(Class1));
st1 = ptr->MyVBFunction(&st);
}
catch(_com_error &e)
{
bstrDesc = e.Description();
}
CoUninitialize();
}
it says _Class1Ptr is unknown!
BSTR bstrDesc;
try
{
HRESULT hr= CoInitialize(NULL);
CLSID clsid;
hr = CLSIDFromProgID(OLESTR("<complete class name as see in registry>"),&clsid);
short st = 2;
short st1;
//nameOfClassInOCX is placeholder for explanation. If you OCX com class name is blabla
//use _blabla and so on.
_nameOfClassInOCX * ptr;
hr = CoCreateInstance(clsid,NULL,CLSCTX_INPROC_SERVER,__uuidof(_nameOfClassInOCX ),(LPVOID*)&ptr);
cout << ptr->GetFees("hi") <<endl;
ptr->Release();
}
catch(_com_error &e)
{
bstrDesc = e.Description();
}
CoUninitialize();
First of all you have to do this is #import the dll, and the compiler will automatically generate all required definitions from it. Then create objects from the library by using either smart pointers, or CreateInstance().
#import "C:\files\test.dll" no_namespace rename("EOF", "EOFile")
...
int main() {
if (FAILED(::CoInitialize(NULL)))
return 0;
........
::CoUninitialize();
return 0;
}
I'm trying to use WTL within an in-process COM server DLL (an IE BHO), but am struggling with _Module.
My DLL needs CMyModule derived from CAtlDllModuleT<>:
class CMyModule : public CAtlDllModuleT< CMyModule >
{
public:
DECLARE_LIBID(LIBID_MyLib)
DECLARE_REGISTRY_APPID_RESOURCEID(IDR_MYPROJ, "{...}")
};
CMyModule _Module;
extern "C" BOOL WINAPI DllMain(...)
{
hInstance;
return _Module.DllMain(dwReason, lpReserved);
}
...
STDAPI DllUnregisterServer(void)
{
return _Module.DllUnregisterServer();
}
But this conflicts with most WTL examples, which require something like this within stdafx.h:
extern CAppModule _Module; // WTL version of CComModule
No matter which way I do it, I (unsurprisingly) get compile errors. CMyModule derived from CAppModule borks on _Module.DllUnregisterServer(), etc. CMyModule derived from CAtlDllModuleT<> borks on code like _Module.GetMessageLoop().
Any good references on how WTL is supposed to work within a DLL? Google finds lots of questions, with few answers.
I have a project that uses WTL in a DLL. I looked at how my headers are set up and it looks like I hacked around this same problem...
I have my module set up like your sample code inheriting from CAtlDllModuleT<> except the name of the global module variable is _AtlModule rather than _Module. For example:
class CMyModule : public CAtlDllModuleT< CMyModule >
{
public:
DECLARE_LIBID(LIBID_MyLib)
DECLARE_REGISTRY_APPID_RESOURCEID(IDR_MYPROJ, "{...}")
};
CMyModule _AtlModule;
So, all of the DllMain.cpp entry points use _AtlModule. Then in the stdafx.h file it looks like this:
// WTL includes
#define _Module (*_pModule)
#include <atlapp.h>
#include <atlctrls.h>
#include <atldlgs.h>
#undef _Module
That _pModule thing is defined in atlbase.h like:
__declspec(selectany) CComModule* _pModule = NULL;
There must be a better way, but this does work.
Have you considered the option of multiple inheritance? Try inheriting from both CAtlDllModule and CAppModule since you need both.
I use WTL in an Office add-in; the following works for me. (At the bottom of stdafx.h)
class DECLSPEC_UUID("XXXX-...") MyLib;
using namespace ATL;
/*
* Application module
*/
class CAddInModule : public CAtlDllModuleT< CAddInModule >
{
public:
CAddInModule() : m_hInstance(NULL)
{
}
DECLARE_LIBID(__uuidof(MyLib))
HINSTANCE GetResourceInstance()
{
return m_hInstance;
}
void SetResourceInstance(HINSTANCE hInstance)
{
m_hInstance = hInstance;
}
private:
HINSTANCE m_hInstance;
};
extern CAddInModule _AtlModule;
And then the DLL main use _AtlModule:
// DLL Entry Point
extern "C" BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
_AtlModule.SetResourceInstance(hInstance);
return _AtlModule.DllMain(dwReason, lpReserved);
}
// Used to determine whether the DLL can be unloaded by OLE
STDAPI DllCanUnloadNow(void)
{
return _AtlModule.DllCanUnloadNow();
}
// Returns a class factory to create an object of the requested type
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
return _AtlModule.DllGetClassObject(rclsid, riid, ppv);
}
// DllRegisterServer - Adds entries to the system registry
STDAPI DllRegisterServer(void)
{
// registers object, typelib and all interfaces in typelib
HRESULT hr = _AtlModule.DllRegisterServer();
return hr;
}
// DllUnregisterServer - Removes entries from the system registry
STDAPI DllUnregisterServer(void)
{
HRESULT hr = _AtlModule.DllUnregisterServer();
return hr;
}