VB.NET – Native VC++ DLL

15 2018

VC++ Data Type – VB.NET Implementation :

1. LPBYTE – ByRef results As Byte,
2. INT – ByVal a as Int32
3. DWORD – ByVal statusSize As Int32
4. LPDWORD – ByRef len As Long

Sample 1:

'EXTERN_C_DLL_EXPORT INT Function1(BOOL bool_value, LPBYTE longpointer_byte_value, DWORD dword_value, LPDWORD longpointer_dword_value);
<DllImport(DLL_FILENAME, CallingConvention:=CallingConvention.Cdecl)> _
Public Shared Function Function1 _
(ByVal bool_valueAs Boolean,
ByRef longpointer_byte_valueAs Byte,
ByVal dword_valueAs Int32,
ByRef longpointer_dword_valueAs Int32) As Int32
End Function

Sample 2:

VC++

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
<Serializable()> Public Structure ErrorLog
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=2, ArraySubType:=UnmanagedType.U1)> _
Public ucErrorID() As Byte ‘ 2 Byte
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=5, ArraySubType:=UnmanagedType.U1)> _
Public ucTime() As Byte ‘ 5 Byte
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=8, ArraySubType:=UnmanagedType.U1)> _
Public ucErrorCode() As Byte ‘ 8 Byte
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=2, ArraySubType:=UnmanagedType.U1)> _
Public ucStatusSts() As Byte ‘ 2 Byte
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=22, ArraySubType:=UnmanagedType.U1)> _
Public ucSensorSts() As Byte ‘ 22 Byte
Public ucCmdNo As Byte

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=28, ArraySubType:=UnmanagedType.U1)> _
Public dummy() As Byte ‘ 28 Byte
End Structure

typedef struct _fullLog {
ERRORLOG errLog[5];
} FULLLOG;
VB.NET
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
<Serializable()> Public Structure fulllog
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=512, ArraySubType:=UnmanagedType.AsAny)> _
Public Error_Log() As ErrorLog
End Structure

‘EXTERN_C_DLL_EXPORT INT GetMylLog(FULLLOG *fullLog);
<DllImport(DLL_FILENAME, CallingConvention:=CallingConvention.Cdecl)> _
Public Shared Function GetMylLog(ByRef FullLog1 As ErrorLog, ByVal dwLen As UInt32, ByRef lpLen As UInt32, <MarshalAs(UnmanagedType.LPWStr)> Optional lpPathName As String = “”) As Int32
End Function

 

Unicode CString to char *

06 2013

CString str
wchar_t* wchar_str;
char* char_str;
int char_str_len;

//1. CString to wchar_t* conversion
wchar_str = str.GetBuffer(str.GetLength());

//2. wchar_t* to char* conversion
char_str_len = WideCharToMultiByte(CP_ACP, 0, wchar_str, -1, NULL, 0, NULL, NULL);
char_str = new char[char_str_len];

//3.wchar_t* to char* conversion
WideCharToMultiByte(CP_ACP, 0, wchar_str, -1, char_str, char_str_len, 0,0);

Unicode Multi Byte to String

27 2012


int count = strCommandScript.GetLength();
char* c = new char[count + 1];
for(int j = 0; j < count; j++) { c[j] = static_cast (* strCommandScript.Mid(j, 1));
}
c[count] = '\0';

Find String and Highlight in RichEdit MFC

24 2012

This is just the sample code snippet.
Code standards are not followed. Sorry for that.

void CDisplayDlg::OnEnChangeRichedit21()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.

// TODO: Add your control notification handler code here
CHARRANGE selectionRange;
int min =0;
m_RichEditCtrl.SendMessage(EM_EXGETSEL, 0, (LPARAM)&selectionRange);
long len = m_RichEditCtrl.GetTextLength();
long lres = 0;
int i = 0 ;
do
{
i++;
FINDTEXTEX ftex;
ftex.lpstrText = _T("1b 2e");
ftex.chrg.cpMin = min ;//selectionRange.cpMin;
ftex.chrg.cpMax = -1;//selectionRange.cpMax;
lres = m_RichEditCtrl.FindTextW(FR_DOWN, &ftex);
if(lres > 0)
{
min = lres +1;
CHARFORMAT2 FORMAT;
::memset( &FORMAT, 0, sizeof( FORMAT ) );
FORMAT.cbSize = sizeof( FORMAT );
FORMAT.dwMask = CFM_BACKCOLOR;

FORMAT.crBackColor = RGB(255,255,0);
m_RichEditCtrl.SetSel( lres, lres+5 );
m_RichEditCtrl.SendMessage(EM_SETCHARFORMAT,(WPARAM) SCF_SELECTION, (LPARAM) &FORMAT );
m_RichEditCtrl.SetSel(0,0);
}

}while(i< len); }

Set Minimum Window Size while Dragging Window in VC++

21 2012

The following code snippet is just to set the minimum size for a window while resizing.
This avoids dragging or flickering of the window.

void CMyDlg::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
// TODO: Add your message handler code here and/or call default
lpMMI->ptMinTrackSize.x = 640;
lpMMI->ptMinTrackSize.y = 480;
CDialog::OnGetMinMaxInfo(lpMMI);
}

Make Alpha Transparent Dialog in MFC

23 2011

// Enable WS_EX_LAYERED window extended style.
LONG ExtendedStyle = GetWindowLong( GetSafeHwnd(), GWL_EXSTYLE );
SetWindowLong( GetSafeHwnd(), GWL_EXSTYLE, ExtendedStyle | WS_EX_LAYERED );

// Select the transparency percentage.
// The alpha will be calculated accordingly.
double TransparencyPercentage = 50.0;

// Set the alpha for transparency.
// 0 is transparent and 255 is opaque.
double fAlpha = TransparencyPercentage * ( 255.0 /100 );
BYTE byAlpha = static_cast( fAlpha );
SetLayeredWindowAttributes( GetSafeHwnd(),
0,
byAlpha,
LWA_ALPHA );