Benny K
Benny K

Reputation: 2087

Calling C++ function with default parameters from Python

I have exported this simple function from an DLL (Am working in MSVC2015, Windows 10, x64) :

// Main.h
#define DLL_SEMANTICS __declspec (dllexport)
extern "C"
{
    DLL_SEMANTICS int CheckVal(const int x, const int y = 1);

}

code:

// Main.cpp

    int CheckVal(const int x, const int y)
    { cout << y << endl; return 0; }

}

According to this SO thread using extern "C" shouldn't be a problem and indeed when calling this function from an exe file I got the desired results (i.e., "1" to the console) but but when I called this from Python using:

import ctypes
from ctypes import cdll

dllPath = r"C:\MyDll.dll"
lib = cdll.LoadLibrary(dllPath)
lib.CheckVal(ctypes.c_int(1))

I am getting some garbage value being printed (Debugging via PTVS confirmed that y is indeed garbage value.)

Am I doing something wrong or defualt parameters cannot work from Python?

Upvotes: 1

Views: 285

Answers (1)

Botje
Botje

Reputation: 31193

Default parameters are a nicety of C++. You need to pass two arguments if you're calling outside C++.

Upvotes: 1

Related Questions