Just trying to copy a single font file to the C:\Windows\Fonts
folder using this specific method (I do already know about the other various methods of copying files).
Here's my code, issues are mentioned below the code:
Declarations:
Private Structure SHFILEOPSTRUCT
Dim hwnd As Integer
Dim wFunc As Integer
Dim pFrom As String
Dim pTo As String
Dim fFlags As Short
Dim fAnyOperationsAborted As Boolean
Dim hNameMappings As Integer
Dim lpszProgressTitle As String
End Structure
<DllImport("shell32.dll", EntryPoint:="SHFileOperation", CharSet:=CharSet.Auto, SetLastError:=True, ThrowOnUnmappableChar:=True)>
Private Function SHFileOperation(ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
End Function
Private Const FO_COPY As Integer = &H2S
Private Const FOF_NOCONFIRMATION As Integer = &H10S
Private Const FOF_SILENT As Integer = &H4S
Function:
Dim shf As SHFILEOPSTRUCT
Dim strWinFontFolder As String = Environment.ExpandEnvironmentVariables("%WINDIR%" & "\Fonts")
With shf
.wFunc = FO_COPY
.pFrom = String.Format("{0}{1}{1}", strFontPath, vbNullChar)
.pTo = String.Format("{0}{1}{1}", strWinFontFolder, vbNullChar)
.fFlags = FOF_NOCONFIRMATION Or FOF_SILENT
.lpszProgressTitle = String.Format("Sending {0} to the Font Folder", strFontPath)
End With
Try
SHFileOperation(shf)
Catch ex As Exception
Debug.WriteLine(String.Format("SHFILEOPSTRUCT: {0}", ex.Message))
End Try
Remark from MS Docs:
Important You must ensure that the source and destination paths are double-null terminated. A normal string ends in just a single null character. If you pass that value in either the source or destination members, the function will not realize when it has reached the end of the string and will continue to read on in memory until it comes to a random double null value. This can at least lead to a buffer overrun, and possibly the unintended deletion of unrelated data.
Issues:
When I double null the endings of both the .pTo
and .pFrom
lines as in the remark, nothing at all seems to happen and the file isn't copied. No errors, nothing at all. Crickets.
When I accidentally only single null terminated the endings, I got this error (on my English system, no idea why it shows Asian characters?)
I should also note that Before this function is even called, I do a:
If File.Exists(strFontPath) = True Then (yada yada yada)
and the file does indeed exist.
Anyone know why it won't copy?