Error Info


Error LNK1169 one or more multiply defined symbols found SAMPLE C:\SAMPLE\Debug\SAMPLE.dll 1

Error LNK2005 _DllMain@12 already defined in dllmain.obj SAMPLE C:\SAMPLE\mfcs140d.lib(dllmodul.obj) 1




Solution


Project -> Properties -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions


Remove "_USRDLL;"



Error Info


Severity Code Description Project File Line Suppression State

Error C1189 #error:  Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d] CMM_DES c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\atlmfc\include\afx.h 24



Solution


Add 


#define _AFXDLL 


To stdafx.h

Cpp 와 CSharp 사이에 메모리 통신을 구현하는 코드 입니다. 



실행화면은 다음과 같습니다. 




FileMappingNative Class 에서 Kernel32.dll 을 Import 하여 구현합니다. 



CSharp:


Common:

1
2
3
4
const uint BUFFER_SIZE = 256;
IntPtr pBuf;
IntPtr hMapFile;
string strMapFileName = "SharedMemory_CPP_CSHARP";
cs



Server:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Create the file mapping object
hMapFile = FileMappingNative.CreateFileMapping(
    (IntPtr)FileMappingNative.INVALID_HANDLE_VALUE,
    IntPtr.Zero,
    // 파일 Mapping 을 쓰기 가능으로 Open
    FileProtection.PAGE_READWRITE,
    0,
    BUFFER_SIZE,
    strMapFileName);
    
// Create file view from the file mapping object.
// 버퍼에 매핑. 향후 pBuf 를 통하여 메모리에 쓸 수 있음
pBuf = FileMappingNative.MapViewOfFile(
    hMapFile,
    FileMapAccess.FILE_MAP_ALL_ACCESS,
    0,
    0,
    BUFFER_SIZE);
    
// 메모리에 할당할 내용
string strMessage = rand.Next(100).ToString("00");
// 유니코드 바이트 배열로 변환 
byte[] bMessage = Encoding.Unicode.GetBytes(strMessage);
// pBuf 에 내용을 씀
Marshal.Copy(bMessage, 0, pBuf, bMessage.Length);
cs



Client:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Open the named file mapping.
hMapFile = FileMappingNative.OpenFileMapping(
    FileMapAccess.FILE_MAP_ALL_ACCESS,
    false,
    strMapFileName);
    
pBuf = FileMappingNative.MapViewOfFile(
    hMapFile,
    FileMapAccess.FILE_MAP_ALL_ACCESS,
    0,
    0,
    BUFFER_SIZE);
    
string strMessage = Marshal.PtrToStringUni(pBuf);
cs



CPP:


Common:


1
2
3
4
#define BUF_SIZE 256
TCHAR szName[] = TEXT("SharedMemory_CPP_CSHARP");
HANDLE hMapFile;
LPCTSTR pBuf;
cs



Server:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
hMapFile = CreateFileMapping(
    INVALID_HANDLE_VALUE,    // use paging file
    NULL,                    // default security
    PAGE_READWRITE,          // read/write access
    0,                       // maximum object size (high-order DWORD)
    BUF_SIZE,                // maximum object size (low-order DWORD)
    szName);                 // name of mapping object
 
pBuf = (LPTSTR)MapViewOfFile(hMapFile,   // handle to map object
    FILE_MAP_ALL_ACCESS, // read/write permission
    0,
    0,
    BUF_SIZE);
 
//쓸 값    
int nTemp = rand() % 100;
wchar_t msg[256];
//%02 To Need MemoryOverWrite
swprintf_s(msg, L"%02d", nTemp);
 
const wchar_t *cMsg = const_cast<wchar_t*>(msg);
 
//pBuf 에 내용을 쓴다
CopyMemory((PVOID)pBuf, cMsg, (_tcslen(cMsg) * sizeof(TCHAR)));
cs



Client:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//MapFile 을 Open
hMapFile = OpenFileMapping(
    FILE_MAP_ALL_ACCESS,   // read/write access
    FALSE,                 // do not inherit the name
    szName);               // name of mapping object
 
//pBuf 에 매핑. 향후 pBuf 를 통하여 메모리에 쓸 수 있음
pBuf = (LPTSTR)MapViewOfFile(hMapFile, // handle to map object
    FILE_MAP_ALL_ACCESS,  // read/write permission
    0,
    0,
    BUF_SIZE);
 
char pstrDest[256];
int nLen = (int)wcslen(pBuf);
 
#pragma warning(disable:4996)
//원하는 정보는 pstrDest 에 복사됨
wcstombs(pstrDest, pBuf, nLen + 1);
printf("CountDown: %d, ReadMemory: %s\r\n", nCount, pstrDest);
cs



소스코드 링크



이상으로 포스팅을 마칩니다. 

+ Recent posts