C++에서 C#으로 데이터를 전달하고 처리해야하는 프로그램이 있습니다. 이를 위해 구조체를 검색하여 바이트 배열로 변환 한 다음 다른 끝에 다시 변환했습니다. 그러나 메모리 덤프가 각 변수의 메모리 값이 동일 함을 나타내더라도 데이터를 다시 변환 할 때 데이터가 올바르지 않습니다.구조체를 전달하면 데이터가 손상됩니다.
BYTE * cpiBuffer = (BYTE*)calloc(_PublicKey->Length, sizeof(BYTE));
for(int i = 0; i < _PublicKey->Length; i++)
cpiBuffer[i] = _PublicKey[i];
PCERT_PUBLIC_KEY_INFO cpi = (PCERT_PUBLIC_KEY_INFO)cpiBuffer;
이 그들을보고 :
array<Byte>^GetPublicKeyBlob(String^ContainerName) {
const TCHAR * tContainer = context->marshal_as<const TCHAR*>(ContainerName);
HCRYPTPROV hProv = NULL;
CryptAcquireContext(&hProv, tContainer, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_MACHINE_KEYSET);
DWORD dwKeySize = 0;
CryptExportPublicKeyInfo(hProv, AT_SIGNATURE, X509_ASN_ENCODING, NULL, &dwKeySize);
PCERT_PUBLIC_KEY_INFO pbKey = (PCERT_PUBLIC_KEY_INFO)calloc(dwKeySize, sizeof(BYTE));
CryptExportPublicKeyInfo(hProv, AT_SIGNATURE, X509_ASN_ENCODING, (PCERT_PUBLIC_KEY_INFO)pbKey, &dwKeySize);
array<Byte>^retVal = gcnew array<Byte>(dwKeySize);
for(int i = 0; i < dwKeySize; i++)
retVal[i] = ((BYTE*)pbKey)[i];
free(pbKey);
return retVal;
}
그런 다음 다른 쪽 끝에서, 나는 다음과 같은 코드로 다시 PCERT_PUBLIC_KEY_INFO 구조 변경 : 여기
는 값을 검색 할 수있는 코드 메모리 덤프에서 pbKey, retVal, _PublicKey, cpiBuffer 및 cpi는 모두 동일한 값을 갖습니다. 그러나 cpi를 구조체로 볼 때 Algorithm.pszObjId는 잘못된 메모리 위치를 가리키며 함수에서 사용하려고하면 실패합니다. 여기서 내가 뭘 잘못하고 있니?
"Algorithm.pszObjId가 잘못된 메모리 위치를 가리키고 있음"이란 무엇을 의미합니까? 포인터입니까? –
이것은 LPSTR입니다. 시계에서 구조를 확장하면 구조 안의 메모리 위치를 가리키는 반면 원래 값을 얻으면 구조 내의 위치를 가리키고 있습니다. –