我使用p/invoke从我的非托管代码返回一个"DN_OPstruct"数组:
struct DN_OPstruct {
const char* TargetNode_Identifier;
const char* Name;
int TargetNode_NamespaceIndex;
...
};
EXTERN_C UA_EXPORT_WRAPPER_IMPORT int getOpToArr(const char* _rootGuid, DN_OPstruct ** array, int * arraySizeInElements){
std::list<UA_Ref_and_TargetNode> uaList;
uaList = getLisT(...)
*arraySizeInElements = uaList.size();
int bytesToAlloc = sizeof(DN_OPstruct) * (*arraySizeInElements);
DN_OPstruct * a = static_cast<DN_OPstruct*>(CoTaskMemAlloc(bytesToAlloc));
*array = a;
for (UA_Ref_and_TargetNode &i: uaList){
DN_OPstruct iterOp;
iterOp = getOp(...);
opList.push_back(iterOp);
}
return 1;
}
Run Code Online (Sandbox Code Playgroud)
我的托管代码如下所示:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DN_OPstruct
{
private IntPtr TargetNode_Identifier;
private IntPtr NamePtr;
public string Guid …Run Code Online (Sandbox Code Playgroud) 在我正在研究的项目中,我偶然发现了这个问题:
我想创建一个类"ApiID"的实例.我从Reflector获得了代码,因为你可以看到.dll(不是我的Project)导入来自ummanaged代码,我无权访问.
[StructLayout(LayoutKind.Sequential, Size=0x10), CLSCompliant(false), NativeCppClass, DebugInfoInPDB, MiscellaneousBits(0x40)]
public struct ApiId
{
private long <alignment member>;
public static unsafe void <MarshalCopy>(ApiId* idPtr1, ApiId* idPtr2)
{
ApiId.{ctor}(idPtr1, (ApiId modopt(IsConst)* modopt(IsImplicitlyDereferenced)) idPtr2);
}
public static unsafe void <MarshalDestroy>(ApiId* idPtr1)
{
ApiId.{dtor}(idPtr1);
}
}
Run Code Online (Sandbox Code Playgroud)
我的C#-Code看起来像这样:
var _apiId = new ApiId();
long vlue = 0x0000000041403070;
typeof(ApiId).GetField("<alignment member>", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(_apiId, vlue);
Run Code Online (Sandbox Code Playgroud)
该代码成功运行,但是Field不会改变并保持0 ...我做错了什么?问候
嘿伙计们,我想知道控制台在这个简单的C++代码中会显示什么
using namespace std;
class Tier {
public:
string name;
};
int main()
{
Tier Affe;
cout << Affe.name << endl;
}
Run Code Online (Sandbox Code Playgroud)
我来自C#并且知道会抛出"null"-Error.但在C++中,控制台没有显示任何内容.调试属性时是"".
这是否意味着属性"name"引用了nullpointer?考虑从类"Tier"创建对象,将分配具有类大小的内存.因此,对象的属性"名称"也将在内存中分配.
为什么调试器会显示"".在我的意见中应该是一些随机数据,这是在之前的内存部分......