Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics NAV (Archived)

Using CFront with PowerShell, is it possible?

(0) ShareShare
ReportReport
Posted on by

Hi, is it possible to access a Native NAV db using CFront via Powershell? We are looking at automating a data extract of a table but are having trouble connecting to the db. Demo code is posted below. The problem is that ::Instance always returns $null, so we can't access any methods underneath it.

###
# Load Native Database Libraries
#

# Settings
$DriverType = [Microsoft.Dynamics.NAV.CFront.NavisionDriverType]::Native
$HideErrors = $False
$NavisionPath = "C:\ProgramData\Microsoft\Microsoft Dynamics NAV\60\Database"
$HostName = "Localhost"
$NetType = [Microsoft.Dynamics.NAV.CFront.NavisionNetType]::NativeSecureTcp
$DatabaseName = "C:\ProgramData\Microsoft\Microsoft Dynamics NAV\60\Database\database.fdb"
$CacheSize = 2000
$UseCommitBack = $True
$UseNTAuthentication = $True
$UserID = ""
$Password = ""

# Open the connection
[Reflection.Assembly]::LoadWithPartialName("Microsoft.Dynamics.NAV.CFront.CFrontDotNet")
[Microsoft.Dynamics.NAV.CFront.CFrontDotNet]::DriverType = $DriverType
[Microsoft.Dynamics.NAV.CFront.CFrontDotNet]::HideErrors = $HideErrors
[Microsoft.Dynamics.NAV.CFront.CFrontDotNet]::NavisionPath = $NavisionPath
"Open Connection..."
[Microsoft.Dynamics.NAV.CFront.CFrontDotNet]::Instance.ConnectServerAndOpenDatabase($HostName,$NetType,$DatabaseName,$CacheSize,$UseCommitBack,$UseNTAuthentication,$UserID,$Password)


The output of the script is:

GAC    Version        Location                                                         
---    -------        --------                                                          
True   v2.0.50727     C:\Windows\assembly\GAC_MSIL\Microsoft.Dynamics.NAV.CFront.CFrontDotNet\6.0.0.0__31bf3856ad364e35\Microsoft.Dynamics.NAV.CFront.CFrontDotNet.dll  
                                             
Open Connection...

You cannot call a method on a null-valued expression.
At ##Script Name##.ps1:24 char:1
+ [Microsoft.Dynamics.NAV.CFront.CFrontDotNet]::Instance.ConnectServerAndOpenDatab ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull


Any guidance or assistance most welcome.

Thanks,
Jay :)

*This post is locked for comments

  • Community Member Profile Picture
    on at
    RE: Using CFront with PowerShell, is it possible?

    Boom, it works! Just needed to open it in PowerShell (x86)! :D

    Wish I'd thought of that earlier >_<

  • Verified answer
    keoma Profile Picture
    32,727 on at
    RE: Using CFront with PowerShell, is it possible?

    this should solve your problem.

    http://www.mibuso.com/forum/viewtopic.php?t=45388&f=23

    check version of msvcrt.dll & mfc42.dll.

  • Community Member Profile Picture
    on at
    RE: Using CFront with PowerShell, is it possible?

    I would also point out that for NAV 2013, Microsoft released cmdlets so its supported natively by Powershell, but we have a requirement to use PowerShell on NAV 2009 which doesn't appear supported :(

  • Community Member Profile Picture
    on at
    RE: Using CFront with PowerShell, is it possible?

    Have tried different variations of the load assembly command. No luck so far :(

    Latest PowerShell Code:

    ###
    # Load Native Database Libraries
    #
    "Loading Assembly..."
    [Reflection.Assembly]::LoadFile($(Join-Path -Path (Get-Item -PATH ".\" -Verbose).FullName 'Microsoft.Dynamics.NAV.CFront.CFrontDotNet.dll'))
    
    # Settings
    $DriverType = [Microsoft.Dynamics.NAV.CFront.NavisionDriverType]::Native
    $HideErrors = $False
    $NavisionPath = "C:\ProgramData\Microsoft\Microsoft Dynamics NAV\60\Classic"
    $HostName = ""
    $NetType = [Microsoft.Dynamics.NAV.CFront.NavisionNetType]::NativeSecureTcp
    $DatabaseName = "C:\ProgramData\Microsoft\Microsoft Dynamics NAV\60\Database\database.fdb"
    $CacheSize = 2000
    $UseCommitBack = $True
    $UseNTAuthentication = $True
    $UserID = ""
    $Password = ""
    
    # Open the connection
    [Microsoft.Dynamics.NAV.CFront.CFrontDotNet]::DriverType = $DriverType
    [Microsoft.Dynamics.NAV.CFront.CFrontDotNet]::HideErrors = $HideErrors
    [Microsoft.Dynamics.NAV.CFront.CFrontDotNet]::NavisionPath = $NavisionPath
    "Open Connection..."
    [Microsoft.Dynamics.NAV.CFront.CFrontDotNet]::Instance.ConnectServerAndOpenDatabase($HostName,$NetType,$DatabaseName,$CacheSize,$UseCommitBack,$UseNTAuthentication,$UserID,$Password)

    This produces the output:

    Loading Assembly...
    
    GAC    Version        Location                                                                                                                                                                                       
    ---    -------        --------                                                                                                                                                                                       
    False  v2.0.50727     C:\test\Microsoft.Dynamics.NAV.CFront.CFrontDotNet.dll                                                                                                                                         
    Open Connection...
    You cannot call a method on a null-valued expression.
    At C:\test\Microsoft Navision 2009 GL Extract Script v1.0 (Native).ps1:25 char:1
    + [Microsoft.Dynamics.NAV.CFront.CFrontDotNet]::Instance.ConnectServerAndOpenDatab ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull


    You can see its using the local directoy dll's now, but its still not working.

    The working VB.NET code mirrors the non-working PowerShell code above:

    Imports Microsoft.Dynamics.NAV.CFront
    
    Module Module1
    
        Sub Main()
    
            Try
    
                ' configure variables ' C:\ProgramData\Microsoft\Microsoft Dynamics NAV\60\Database\.fdb
                Dim DriverType As NavisionDriverType = NavisionDriverType.Native,
                    HideErrors As Boolean = False,
                    NavisionPath As String = "C:\ProgramData\Microsoft\Microsoft Dynamics NAV\60\Classic",
                    HostName = "",
                    NetType As NavisionNetType = NavisionNetType.NativeSecureTcp,
                    DatabaseName As String = "C:\ProgramData\Microsoft\Microsoft Dynamics NAV\60\Database\database.fdb",
                    CacheSize As Integer = 2000,
                    UseCommitBack As Boolean = True,
                    UseNTAuthentication As Boolean = True,
                    UserID As String = "",
                    Password As String = ""
                Try
                    Microsoft.Dynamics.NAV.CFront.CFrontDotNet.DriverType = DriverType
                    Microsoft.Dynamics.NAV.CFront.CFrontDotNet.NavisionPath = NavisionPath
                    Microsoft.Dynamics.NAV.CFront.CFrontDotNet.HideErrors = HideErrors
                Catch ex As Exception
                    Throw New Exception("Configuration Failure!", ex)
                End Try
                Try
                    Microsoft.Dynamics.NAV.CFront.CFrontDotNet.Instance.ConnectServerAndOpenDatabase(HostName, NetType, DatabaseName, CacheSize, UseCommitBack, UseNTAuthentication, UserID, Password)
                Catch ex As Exception
                    Throw New Exception("Connection failure!", ex)
                End Try
                Console.WriteLine(" + Connected!")
            Catch ex As Exception
                Console.WriteLine(ex.ToString)
    
            End Try
            ' keep the user informed, only close then they want to
            Console.WriteLine("Press any key to close...")
            Console.ReadKey()
        End Sub
    
    End Module
    


    This outputs:

    + Connected...

    Not sure where to look now... :(

  • Suggested answer
    keoma Profile Picture
    32,727 on at
    RE: Using CFront with PowerShell, is it possible?

    hi,

    the powershell also works on a certain local folder. try to run the powershell script out from the same folder as the .net console app.

    and i think the line

    [Reflection.Assembly]::LoadWithPartialName("...

    points to the used working directory. so the assembly has to be in that folder.

    and: LoadWithPartialName is deprecated. better use LodFrom with a defined full path.

    also have a look at

    stackoverflow.com/.../how-to-load-assemblies-in-powershell

  • Community Member Profile Picture
    on at
    RE: Using CFront with PowerShell, is it possible?

    I still haven't had much luck in solving this. Any success in replicating this issue?

  • Suggested answer
    keoma Profile Picture
    32,727 on at
    RE: Using CFront with PowerShell, is it possible?

    hi,

    CFront.dll and CFrontSQL.dll seem to be automations. so they must be registered on every computer you want to use them.

  • Community Member Profile Picture
    on at
    RE: Using CFront with PowerShell, is it possible?

    I think you are right there. The .NET console app uses the local assembly, while PowerShell uses the GAC assembly. CFrontDotNet relies on CFront.dll and CFrontSQL.dll which can not be added to GAC.

    It is odd though that when testing on a machine without CFrontDotNet dll within GAC - therefore using the local copy, it still threw the same error :(

  • Suggested answer
    keoma Profile Picture
    32,727 on at
    RE: Using CFront with PowerShell, is it possible?

    hi,

    it also can be that there is a missing reference (another assembly used by Microsoft.Dynamics.NAV.CFront.CFrontDotNet.dll) when calling through GAC but works in local folder because all needed assemblies are there?!

  • Community Member Profile Picture
    on at
    RE: Using CFront with PowerShell, is it possible?

    Hi Jonathan,

    Thanks for your quick response.

    I have tried reinstalling the SDK, but still no luck. I can see the managed DLL in GAC which its also consistently reporting is there. I've also tried it on a machine without the libraries registered in the GAC and it gets the same error message.

    What is funny, in the same folder, I have a test .net console app which connects just fine using the same information as powershell. This would suggest its a missing reference, limitation or configuration issue relating to powershell that is at fault.

    :(

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics NAV (Archived)

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans