我想创建一种实用程序类,它只包含可由名称类前缀调用的静态方法.看起来我做错了什么:)
这是我的小班:
class FileUtility():
@staticmethod
def GetFileSize(self, fullName):
fileSize = os.path.getsize(fullName)
return fileSize
@staticmethod
def GetFilePath(self, fullName):
filePath = os.path.abspath(fullName)
return filePath
Run Code Online (Sandbox Code Playgroud)
现在我的"主要"方法:
from FileUtility import *
def main():
path = 'C:\config_file_list.txt'
dir = FileUtility.GetFilePath(path)
print dir
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:unbound method GetFilePath() must be called with FileUtility instance as first argument (got str instance instead).
这里有一些问题:
TypeError: GetFilePath() takes exactly 1 argument (2 given)新的main:
from FileUtility import *
def main():
objFile = FileUtility()
path …Run Code Online (Sandbox Code Playgroud) 我是C++ DLL导入主题的新人,可能是我的问题很容易但我在谷歌上找不到它.
我有一个非常简单的C++ win32 dll:
#include <iostream>
using namespace std;
extern "C"
{
__declspec(dllexport) void __stdcall DisplayHellowFromDLL()
{
cout<<"Hi"<<endl;
}
}
Run Code Online (Sandbox Code Playgroud)
当我从C#调用此方法时,我没有任何问题,这里是C#代码
namespace UnmanagedTester
{
class Program
{
[DllImport(@"C:\CGlobalDll")]
public static extern void DisplayHellowFromDLL();
static void Main(string[] args)
{
Console.WriteLine("This is C# program");
DisplayHellowFromDLL();
}
}
}
Run Code Online (Sandbox Code Playgroud)
正如我所料,输出是:"这是C#程序""嗨".
现在,如果我将C函数的声明更改为:
__declspec(dllexport) void DisplayHellowFromDLL()
Run Code Online (Sandbox Code Playgroud)
没有__stdcall,我也没有任何问题,问题是:
我什么时候才真正需要__declspec(dllexport)TYPE __stdcall,何时我只能使用__declspec(dllexport)TYPE?
非常感谢.
我有一个非托管的C++ DLL,我无法访问代码,但有所有方法声明.
为简单起见,说.h看起来像这样:
#include <iostream>
#ifndef NUMERIC_LIBRARY
#define NUMERIC_LIBRARY
class Numeric
{
public:
Numeric();
int Add(int a, int b);
~Numeric();
};
#endif
Run Code Online (Sandbox Code Playgroud)
和.cpp文件中的方法实现
int Numeric::Add(int a, int b)
{
return (a + b);
}
Run Code Online (Sandbox Code Playgroud)
我只想在C#代码中调用C++中的add函数:
namespace UnmanagedTester
{
class Program
{
[DllImport(@"C:\CPP and CSharp Project\UnmanagedNumeric\Debug\numeric.dll", EntryPoint = "Add")]
public static extern int Add(int a, int b);
static void Main(string[] args)
{
int sum = Add(2, 3);
Console.WriteLine(sum);
}
}
}
Run Code Online (Sandbox Code Playgroud)
尝试执行后,我有以下错误:
无法在DLL'C:\ CPP和CSharp Project\UnmanagedNumeric\Debug \numeric.dll'中找到名为"添加"的入口点.
我不能改变C++代码.不知道出了什么问题.感谢您的帮助.